Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
You are given an array bulbs of integers between 1 and 100.
There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially.
For each element bulbs[i] in the array bulbs:
bulbs[i]th light bulb is currently off, switch it on.Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.
Example 1:
Input: bulbs = [10,30,20,10]
Output: [20,30]
Explanation:
bulbs[0] = 10th light bulb is currently off. We switch it on.bulbs[1] = 30th light bulb is currently off. We switch it on.bulbs[2] = 20th light bulb is currently off. We switch it on.bulbs[3] = 10th light bulb is currently on. We switch it off.Example 2:
Input: bulbs = [100,100]
Output: []
Explanation:
bulbs[0] = 100th light bulb is currently off. We switch it on.bulbs[1] = 100th light bulb is currently on. We switch it off.Constraints:
1 <= bulbs.length <= 1001 <= bulbs[i] <= 100Problem summary: You are given an array bulbs of integers between 1 and 100. There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially. For each element bulbs[i] in the array bulbs: If the bulbs[i]th light bulb is currently off, switch it on. Otherwise, switch it off. Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
[10,30,20,10]
[100,100]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3842: Toggle Light Bulbs
class Solution {
public List<Integer> toggleLightBulbs(List<Integer> bulbs) {
int[] st = new int[101];
for (int x : bulbs) {
st[x] ^= 1;
}
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < st.length; ++i) {
if (st[i] == 1) {
ans.add(i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3842: Toggle Light Bulbs
func toggleLightBulbs(bulbs []int) []int {
st := make([]int, 101)
for _, x := range bulbs {
st[x] ^= 1
}
ans := make([]int, 0)
for i := 0; i < 101; i++ {
if st[i] == 1 {
ans = append(ans, i)
}
}
return ans
}
# Accepted solution for LeetCode #3842: Toggle Light Bulbs
class Solution:
def toggleLightBulbs(self, bulbs: list[int]) -> list[int]:
st = [0] * 101
for x in bulbs:
st[x] ^= 1
return [i for i, x in enumerate(st) if x]
// Accepted solution for LeetCode #3842: Toggle Light Bulbs
fn toggle_light_bulbs(bulbs: Vec<i32>) -> Vec<i32> {
use std::collections::HashSet;
let mut s = HashSet::new();
for b in bulbs {
if s.contains(&b) {
s.remove(&b);
} else {
s.insert(b);
}
}
let mut ret: Vec<_> = s.into_iter().collect();
ret.sort_unstable();
ret
}
fn main() {
let ret = toggle_light_bulbs(vec![10, 30, 20, 10]);
println!("ret={ret:?}");
}
#[test]
fn test() {
assert_eq!(toggle_light_bulbs(vec![10, 30, 20, 10]), [20, 30]);
assert_eq!(toggle_light_bulbs(vec![100, 100]), []);
}
// Accepted solution for LeetCode #3842: Toggle Light Bulbs
function toggleLightBulbs(bulbs: number[]): number[] {
const st: number[] = new Array(101).fill(0);
for (const x of bulbs) {
st[x] ^= 1;
}
const ans: number[] = [];
for (let i = 0; i < 101; i++) {
if (st[i] === 1) {
ans.push(i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.