Data Structures & AlgorithmsModerateTCS NQT · Infosys
Problem: Starting from the string "00000" of five zeros, operations flip a substring: each operation chooses positions l ≤ r and flips every bit in between (0 becomes 1 and 1 becomes 0). The operations are: flip [0,2], flip [1,4], flip [2,2], flip [0,1]. How many 1s are in the final string?
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: B. 4
Each flip is an XOR on a range; simulating it bit by bit gives the final state. Counting only the cells touched once (ignoring the parity from overlapping flips) gives the wrong total.
More Coding Interview Problems questions
- Problem: The number of ways to choose 3 vertices from a graph's vertex set of size n = 12 such that no two chosen vertices are adjacent in…
- Problem: A stream of 12 numbers 5, 1, 4, 4, 2, 5, 3, 1, 2, 3, 3, 4 arrives. How many distinct values appear at least twice in the stream?
- Problem: Start with x = 5. Repeat this rule: if x is odd, set x = x + 3; if x is even, set x = 2x − 1. Count how many times the rule is…
- Problem: Starting from 0 on a number line, a frog jumps +4, then −1, then +4, then −1 and so on, alternating, for 25 jumps in total. It…
- Problem: Count the vertices of the tree with edges 0–1, 0–2, 1–3, 1–4, 2–5, 5–6, 5–7 whose removal leaves every remaining connected…
- Problem: Vertices 1–6 have these directed prerequisites: 1→4, 2→4, 2→5, 3→5, 4→6, 5→6. Among all valid topological orders, the…