Mid/SeniorBrainteaserMath

Bulb Switcher

There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.

On the third round, you toggle every third bulb, turning it on if it is off or turning it off if it is on. For the i^th round, you toggle every i bulb. For the n^th round, you only toggle the last bulb.

Return the number of bulbs that are on after n rounds.

Example 1
Inputn = 3
Output1
After the three rounds, only the first bulb remains on, so the answer is 1.
Example 2
Inputn = 0
Output0
There are no bulbs to turn on, so the answer is 0.

Constraints

  • 0 <= n <= 10^9

Asked at 9 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate