PPractick

DSA interview question

Maximum Consecutive Ones

Easy · Algorithms · Topics: array

The problem

A server monitor records a binary array, one entry per minute: 1 if the service was up and 0 if it was down. The reliability report needs the longest uninterrupted uptime streak, which is the longest run of consecutive 1s. It's a single pass with a running counter that resets at every 0, and it's the simplest example of the "track the current run, remember the best run" pattern that appears in many harder array problems.

Example: [1, 1, 0, 1, 1, 1] → 3.

Function signature

int findMaxConsecutiveOnes(const vector<int>& nums);  // nums contains only 0s and 1s

Test cases

Input numsOutputExplanation
[1, 1, 0, 1, 1, 1]3The streak at the end
[1, 0, 1, 1, 0, 1]2
[0, 0]0No 1s at all
[1]1
[1, 1, 1, 1]4The whole array
[]0

(Confirm: the array is binary; return 0 when there are no 1s.)

How interviewers phrase it

Clarifying questions to ask

  1. Is the array strictly binary?
  2. What should be returned if there are no 1s?
  3. Is flipping any zeros allowed?

Follow-ups to expect

  1. "Why update the best inside the loop, not only when a 0 appears?"
  2. "What if you can flip at most k zeros?"
  3. "Could you do it with a streaming input?"
  4. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in