Solve array duplicate flags and binary swaps
Company: Salesforce
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates array and string algorithm skills, specifically hash-based frequency tracking for left/right duplicate detection and reasoning about synchronous substring transformations to determine stabilization time.
Flag duplicates on the left and right
Constraints
- 1 ≤ n ≤ 2·10^5
- Each nums[i] fits in a 32-bit signed integer
- Output strings have exactly length n, characters only '0' or '1'
Examples
Input: ([5, 1, 5, 2, 1],)
Expected Output: ["00101", "11000"]
Explanation: Left: indices 2 and 4 repeat an earlier value. Right: index 0's 5 recurs at 2, index 1's 1 recurs at 4; nothing else recurs to its right.
Input: ([7],)
Expected Output: ["0", "0"]
Explanation: Single element: no duplicates on either side.
Input: ([3, 3, 3],)
Expected Output: ["011", "110"]
Explanation: left: positions 1,2 saw a 3 earlier; right: positions 0,1 see a 3 later.
Input: ([1, 2, 3, 4],)
Expected Output: ["0000", "0000"]
Explanation: All distinct, so no duplicates in either direction.
Input: ([-1, -1, 0, 0, -1],)
Expected Output: ["01011", "11100"]
Explanation: Negatives and zeros handled like any other value; left and right computed by the same set logic.
Input: ([4, 4],)
Expected Output: ["01", "10"]
Explanation: Index 1 saw a 4 before; index 0 sees a 4 after.
Hints
- A single left-to-right pass with a hash set of already-seen values fills `left`: before inserting nums[i], check whether it is already in the set.
- Symmetrically, a right-to-left pass with a fresh hash set fills `right`: before inserting nums[i], check whether it is already in the set.
- Two linear passes give O(n) time; no nested loops needed.
Synchronous "01" → "10" replacement until stable
Constraints
- 1 ≤ |s| ≤ 2·10^5
- s contains only the characters '0' and '1'
- Return 0 when s is already stable (no "01" substring)
Examples
Input: ("010110",)
Expected Output: 3
Explanation: 010110 -> 101010 -> 110100 -> 111000; 3 synchronous steps.
Input: ("",)
Expected Output: 0
Explanation: Empty string is already stable.
Input: ("0",)
Expected Output: 0
Explanation: No '1', nothing to move.
Input: ("1",)
Expected Output: 0
Explanation: No '0' after the '1'; already stable.
Input: ("01",)
Expected Output: 1
Explanation: One step turns 01 into 10.
Input: ("0011",)
Expected Output: 3
Explanation: 0011->0101->1010->1100; the trailing '1's queue up behind the leading zeros, taking 3 steps.
Input: ("1100",)
Expected Output: 0
Explanation: Already in stable (1s then 0s) form.
Input: ("0001",)
Expected Output: 3
Explanation: A single '1' behind three zeros must hop each, one per second.
Input: ("010101",)
Expected Output: 3
Explanation: Interleaved pattern stabilizes to 111000 in 3 synchronous steps.
Input: ("00110",)
Expected Output: 3
Explanation: Blocking between the two '1's plus the leading zeros yields 3 steps.
Hints
- Think of each '1' moving left past the '0's in front of it. A '1' with no '0' before it never moves and contributes 0.
- Scan left to right tracking the number of zeros seen so far. When you reach a '1' that has at least one zero before it, its finish time is at least `zeros` (it must hop over that many zeros) but also at least one more than the previous '1''s finish time, because the previous '1' can block it for a second.
- So maintain `t = max(t + 1, zeros)` at each '1' (only when zeros > 0); the answer is the final `t`. This is O(n) and avoids the O(n²) simulation blowup.