Solve Adjacent-Deletion and Sorted-Square Problems
Company: Whatnot
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates skills in string manipulation and pattern-removal algorithms for adjacent duplicates and k-sized group deletions, as well as array processing and merging techniques for producing sorted squares; it tests understanding of relevant data-structure concepts and merging/two-pointer strategies within the Coding & Algorithms domain. It is commonly asked to assess algorithmic efficiency, correctness under large input constraints and tricky edge cases (such as cascading deletions and negative values), and is primarily a practical implementation task that emphasizes complexity analysis over purely theoretical reasoning.
Part 1: Remove Adjacent Duplicate Pairs
Constraints
- 0 <= s.length <= 200000
- s contains lowercase English letters
Examples
Input: "abbaca"
Expected Output: "ca"
Explanation: Remove `bb` to get `aaca`, then remove `aa` to get `ca`.
Input: "azxxzy"
Expected Output: "ay"
Explanation: Removing `xx` creates `azzy`, then removing `zz` leaves `ay`.
Input: ""
Expected Output: ""
Explanation: Edge case: an empty string stays empty.
Input: "aaaa"
Expected Output: ""
Explanation: Remove the first `aa`, then the remaining `aa`.
Hints
- Try processing the string from left to right while keeping the characters that have not been canceled yet.
- If the current character matches the most recent kept character, both should disappear.
Part 2: Remove Adjacent Groups of Size k
Constraints
- 1 <= s.length <= 200000
- 2 <= k <= 100000
- s contains lowercase English letters
Examples
Input: ("deeedbbcccbdaa", 3)
Expected Output: "aa"
Explanation: Delete `eee`, then `ccc`, then `bbb`, then `ddd`, leaving `aa`.
Input: ("pbbcggttciiippooaais", 2)
Expected Output: "ps"
Explanation: Repeated deletions of adjacent pairs eventually reduce the string to `ps`.
Input: ("abcd", 2)
Expected Output: "abcd"
Explanation: No adjacent group of size 2 is ever formed.
Input: ("aaa", 3)
Expected Output: ""
Explanation: The whole string is one removable group.
Input: ("a", 2)
Expected Output: "a"
Explanation: Edge case: a single character cannot form a group of size 2.
Hints
- Instead of storing every character separately, track each run as `(character, current_count)`.
- When a run reaches size `k`, remove it immediately so earlier runs can potentially connect.
Part 3: Merge Sorted Arrays into Sorted Squares (Nonnegative Inputs)
Constraints
- 0 <= a.length, b.length <= 200000
- Each array is sorted in nondecreasing order
- All values in `a` and `b` are nonnegative
- The result must be sorted in nondecreasing order
Examples
Input: ([1, 3, 5], [2, 4])
Expected Output: [1, 4, 9, 16, 25]
Explanation: Square each array and merge the results.
Input: ([], [])
Expected Output: []
Explanation: Edge case: both arrays are empty.
Input: ([], [0, 2, 2])
Expected Output: [0, 4, 4]
Explanation: Only the second array contributes values.
Input: ([0, 1, 2], [3])
Expected Output: [0, 1, 4, 9]
Explanation: All squared values remain in sorted order after merging.
Hints
- Because all numbers are nonnegative, squaring does not change the order inside each array.
- Think of this as merging two already sorted arrays, except the values you compare are squares.
Part 4: Merge Sorted Arrays into Sorted Squares (Negative Numbers Allowed)
Constraints
- 0 <= a.length, b.length <= 200000
- Each array is sorted in nondecreasing order
- Values may be negative, zero, or positive
- The result must be sorted in nondecreasing order
Examples
Input: ([-4, -1, 3], [-2, 0, 5])
Expected Output: [0, 1, 4, 9, 16, 25]
Explanation: After squaring and sorting, the merged result is `[0, 1, 4, 9, 16, 25]`.
Input: ([-7, -3, -1], [])
Expected Output: [1, 9, 49]
Explanation: Edge case: only one array is non-empty, and all values are negative.
Input: ([-2, -2, 0], [1, 2])
Expected Output: [0, 1, 4, 4, 4]
Explanation: Duplicates and zero must be handled correctly.
Input: ([], [])
Expected Output: []
Explanation: Edge case: both arrays are empty.
Input: ([-1], [0])
Expected Output: [0, 1]
Explanation: Single-element arrays still need correct ordering after squaring.
Hints
- Within a single sorted array, the largest square comes from one of the two ends.
- You can first compute the sorted squares of each array in linear time, then merge the two sorted results.