Solve windowed duplicates and target expression
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in windowed duplicate detection and time-space trade-offs for streaming or bounded-range scans, along with combinatorial expression generation and on-the-fly numeric evaluation using search, reflecting skills in algorithm design, data-structure selection, and numeric correctness.
Windowed Duplicate Check with Minimum Index Gap
Constraints
- 0 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- 0 <= k <= len(nums)
- When no qualifying pair exists, return [False, -1]
- The minimum index gap is always >= 1 when a pair exists
Examples
Input: ([1, 2, 3, 1], 3)
Expected Output: [True, 3]
Explanation: nums[0]==nums[3]==1 with gap 3 <= 3, so a pair exists and the minimum gap is 3.
Input: ([1, 0, 1, 1], 1)
Expected Output: [True, 1]
Explanation: Indices 2 and 3 both hold 1 with gap 1 <= 1; that is the smallest possible gap.
Input: ([1, 2, 3, 1, 2, 3], 2)
Expected Output: [False, -1]
Explanation: Every duplicate (the 1s, 2s, 3s) is exactly 3 apart, which exceeds k=2, so no qualifying pair exists.
Input: ([], 0)
Expected Output: [False, -1]
Explanation: Empty array has no elements and therefore no pairs.
Input: ([5], 3)
Expected Output: [False, -1]
Explanation: A single element cannot form a pair.
Input: ([1, 1, 1, 1], 2)
Expected Output: [True, 1]
Explanation: Adjacent equal elements give gap 1, the minimum possible.
Input: ([-2, -2, 4, -2], 3)
Expected Output: [True, 1]
Explanation: Indices 0 and 1 hold -2 with gap 1; negatives are handled the same as any other value.
Input: ([4, 5, 6, 4], 0)
Expected Output: [False, -1]
Explanation: The only duplicate (4 at indices 0 and 3) is gap 3, but k=0 requires gap <= 0, so no pair qualifies.
Hints
- Storing the most recent index of each value is enough: for any value, the closest pair of equal entries is always the two consecutive occurrences, so comparing each element only against its previous occurrence finds the global minimum gap.
- To bound space to O(k), keep a sliding window — drop the entry for the element that falls out of range as the index advances (a hash set of the last k values, or a hash map plus a front pointer). Scanning the whole map to evict on every step would push time to O(n*k); the front pointer keeps it O(n).
- Track the best (smallest) gap seen so far; you can stop early only if you find a gap of 1, which is the minimum possible.
Target Expression from Digits 1 to 9
Constraints
- The digit string is fixed as "123456789"
- -10^9 <= target <= 10^9
- Allowed inserts between digits: '+', '-', or nothing (concatenation)
- A leading '+' or '-' may precede the first digit
- Return the empty string "" when no expression evaluates to target
- The returned expression, when evaluated, must equal target exactly
Examples
Input: (100,)
Expected Output: '123+45-67+8-9'
Explanation: 123 + 45 - 67 + 8 - 9 = 100; this is the first expression found under the concat-first branch order.
Input: (45,)
Expected Output: '12+34-5-6-7+8+9'
Explanation: 12 + 34 - 5 - 6 - 7 + 8 + 9 = 45.
Input: (0,)
Expected Output: '12+34-56-7+8+9'
Explanation: 12 + 34 - 56 - 7 + 8 + 9 = 0; a target of zero is reachable.
Input: (1,)
Expected Output: '12+34+5-67+8+9'
Explanation: 12 + 34 + 5 - 67 + 8 + 9 = 1.
Input: (-45,)
Expected Output: '12+3+4-56-7+8-9'
Explanation: 12 + 3 + 4 - 56 - 7 + 8 - 9 = -45; negative targets are reachable using subtraction.
Input: (363,)
Expected Output: '-12+345+6+7+8+9'
Explanation: -12 + 345 + 6 + 7 + 8 + 9 = 363; here a leading '-' is required because no implicit-plus arrangement reaches 363 first.
Input: (123456789,)
Expected Output: '123456789'
Explanation: Concatenating every digit with no operators yields the maximum value 123456789.
Input: (99999,)
Expected Output: ''
Explanation: No insertion of +, -, or concatenation over 1..9 evaluates to 99999, so the empty string is returned.
Input: (7,)
Expected Output: '1+23+4-5-6+7-8-9'
Explanation: 1 + 23 + 4 - 5 - 6 + 7 - 8 - 9 = 7.
Hints
- Carry the running total through the recursion instead of building then re-parsing. To support concatenation you must remember the signed value of the operand currently being built, so that fusing a new digit means subtracting the old operand from the total and adding back the operand grown by one digit (last_sign * (last_num*10 + d)).
- Branch order makes the result deterministic: at each gap try concatenation, then '+', then '-'. For the very first digit, try it as a plain positive operand first, and only fall back to an explicit leading '+' or leading '-' if nothing else reaches the target.
- Pruning: this input is small, but in general you can bound the reachable range — once the partial total plus the maximum positive contribution of the remaining digits is below target (or partial minus the maximum negative contribution is above target), that branch can be abandoned. Watch for integer overflow when concatenating many digits in lower-level languages (use 64-bit accumulators).