Count Moments When Every Turned-On Bulb Is Blue

Quick Overview

Bulbs in a row are switched on one at a time in a given order, and a lit bulb is blue only when every bulb to its left is lit too. The task is to count the moments when every lit bulb is blue. It tests reasoning about prefixes of a permutation and writing a single linear-time scan.

Count Moments When Every Turned-On Bulb Is Blue

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A room has `n` bulbs numbered `1` to `n`, arranged in a row from left to right. All bulbs start turned off. At moment `k` (for `k` from `0` to `n - 1`), bulb `light[k]` is turned on. A bulb is blue when it is on and every bulb to its left is also on. Return the number of moments at which every turned-on bulb is blue. ### Function Signature ```python def num_times_all_blue(light: list[int]) -> int: ``` ### Rules - Moments are numbered from `0`: `light[k]` is the bulb turned on at moment `k`. - A moment counts when, right after bulb `light[k]` is switched on, every bulb that is on is blue. - A bulb stays on once it is turned on. ### Constraints - `n == len(light)` - `1 <= n <= 5 * 10^4` - `light` is a permutation of `[1, 2, ..., n]` ### Examples ```text Input: light = [2, 1, 3, 5, 4] Output: 3 ``` At moment 0 bulb 2 is not blue because bulb 1 is off, and at moment 3 bulb 5 is on while bulb 4 is off. At moments 1, 2 and 4 the bulbs that are on are exactly 1-2, 1-3 and 1-5, so all of them are blue and the count is 3. ```text Input: light = [3, 2, 4, 1, 5] Output: 2 ``` Bulb 1 is the fourth bulb turned on, so moments 0 to 2 cannot qualify. At moment 3 bulbs 1-4 are on and at moment 4 bulbs 1-5 are on, so both moments count. ```text Input: light = [4, 1, 2, 3] Output: 1 ``` Bulbs 1, 2 and 3 are blue as soon as they come on, but bulb 4 was turned on first and is not blue until bulb 3 arrives at moment 3. Only that last moment counts.

Overview: Bulbs in a row are switched on one at a time in a given order, and a lit bulb is blue only when every bulb to its left is lit too. The task is to count the moments when every lit bulb is blue. It tests reasoning about prefixes of a permutation and writing a single linear-time scan.

Grab logo
Grab
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A room has n bulbs numbered 1 to n, arranged in a row from left to right. All bulbs start turned off.

At moment k (for k from 0 to n - 1), bulb light[k] is turned on. A bulb is blue when it is on and every bulb to its left is also on.

Return the number of moments at which every turned-on bulb is blue.

Function Signature

def num_times_all_blue(light: list[int]) -> int:

Rules

  • Moments are numbered from 0 : light[k] is the bulb turned on at moment k .
  • A moment counts when, right after bulb light[k] is switched on, every bulb that is on is blue.
  • A bulb stays on once it is turned on.

Constraints

  • n == len(light)
  • 1 <= n <= 5 * 10^4
  • light is a permutation of [1, 2, ..., n]

Examples

Input: light = [2, 1, 3, 5, 4]
Output: 3

At moment 0 bulb 2 is not blue because bulb 1 is off, and at moment 3 bulb 5 is on while bulb 4 is off. At moments 1, 2 and 4 the bulbs that are on are exactly 1-2, 1-3 and 1-5, so all of them are blue and the count is 3.

Input: light = [3, 2, 4, 1, 5]
Output: 2

Bulb 1 is the fourth bulb turned on, so moments 0 to 2 cannot qualify. At moment 3 bulbs 1-4 are on and at moment 4 bulbs 1-5 are on, so both moments count.

Input: light = [4, 1, 2, 3]
Output: 1

Bulbs 1, 2 and 3 are blue as soon as they come on, but bulb 4 was turned on first and is not blue until bulb 3 arrives at moment 3. Only that last moment counts.