FastPrepOpen Lockers After Toggle Passes
Problems/Open Lockers After Toggle Passes

Open Lockers After Toggle Passes

Tekion logoTekion● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asked how many of 100 closed lockers stay open after 100 toggle passes and called it a bulb-switcher. The judged task generalizes that rule to n lockers and n passes and matches the visible source at about 95%.

There are n lockers in a hallway, numbered 1 through n. Every locker starts closed.

You make exactly n passes. On pass i (1 <= i <= n), toggle every locker whose number is a multiple of i: a closed locker opens and an open locker closes.

Return how many lockers are open after the final pass.

What the interview report shared

The report asked how many of 100 initially closed lockers remain open after 100 toggle passes, and called the exercise a bulb-switcher variation.

Function

openLockers(n: int) → int

Examples

Example 1

n = 100return = 10

After 100 passes, a locker stays open only when it is toggled an odd number of times. That happens exactly for the perfect squares 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, so 10 lockers are open.

Example 2

n = 3return = 1

Pass 1 opens lockers 1, 2, 3. Pass 2 closes locker 2. Pass 3 closes locker 3. Only locker 1 remains open.

Constraints

  • 1 <= n <= 10^6.

More Tekion problems

  • Peak Resource Usage Across Weighted IntervalsPHONE SCREEN · Seen Sep 2026
  • Largest Rectangle in HistogramONSITE INTERVIEW · Seen Sep 2026
  • Next Greater Element to the RightONSITE INTERVIEW · Seen Sep 2026
All Tekion interview questionsSee Tekion hiring insights
Saved
class Solution {
    public int openLockers(int n) {
      // Write your code here.
    }
}
Checking account…