Drill

ProblemsJavaScript › games

Find the longest winning streak

mediumgamesJavaScript

A sequence of game results is recorded as wins and losses. Find the length of the longest unbroken streak of wins.

winStreak(results: list<bool>) → int

Solve it in the editor →

Where you start

function winStreak(results) {
  
}

Worked examples

CallResult
winStreak([true,true,false,true])2
winStreak([false,false,false])0
winStreak([true,true,true])3
winStreak([])0

Hint

Keep a running counter that resets on every false, and track the best so far.

Reference solution in JavaScript
function winStreak(results) {
  let best = 0, cur = 0;
  for (const r of results) {
    if (r) { cur++; if (cur > best) best = cur; }
    else cur = 0;
  }
  return best;
}

The same problem in another language

More games problems in JavaScript