Solve frequency sort and all-pairs sum
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: Solve frequency sort and all-pairs sum evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Frequency-Based String Reorder
Constraints
- 0 <= len(s) <= 10^5
- s consists of printable ASCII characters
- Equal-frequency characters MUST be emitted in ascending ASCII order
- Each character appears in the output exactly as many times as in the input
Examples
Input: ('tree',)
Expected Output: 'eert'
Explanation: 'e' freq 2 first; 'r'(114) and 't'(116) tie at 1 → ascending char code.
Input: ('cccaaa',)
Expected Output: 'aaaccc'
Explanation: 'a'(97) and 'c'(99) both freq 3 → tie broken by ascending char code, 'a' before 'c'.
Input: ('Aabb',)
Expected Output: 'bbAa'
Explanation: 'b' freq 2 first; 'A'(65) before 'a'(97) on the freq-1 tie.
Input: ('',)
Expected Output: ''
Explanation: Empty input → empty output (edge case).
Input: ('a',)
Expected Output: 'a'
Explanation: Single character (edge case).
Input: ('aabbcc',)
Expected Output: 'aabbcc'
Explanation: All three characters tie at freq 2 → emitted in ascending char-code order a,b,c.
Hints
- Count each character's frequency with a hash map (or a fixed-size array of length 256 indexed by char code).
- Sort the distinct characters by the key (-frequency, char_code): negative frequency gives descending frequency, char_code gives the ascending-ASCII tie-break.
- Reconstruct the answer by repeating each character by its count in sorted order.
All Unique Two-Sum Pairs in a Sorted Array
Constraints
- 0 <= len(nums) <= 10^5
- nums is sorted in nondecreasing order
- -10^9 <= nums[i], target <= 10^9
- Report each distinct value-pair at most once (dedup on repeated values)
- Required: O(n) time, O(1) extra space (excluding output), two-pointer method
Examples
Input: ([1, 2, 3, 4, 4, 9, 56, 90], 8)
Expected Output: [[4, 4]]
Explanation: 4+4=8; the duplicate 4 is skipped so [4,4] is reported once.
Input: ([2, 7, 11, 15], 9)
Expected Output: [[2, 7]]
Explanation: Classic single pair, 2+7=9.
Input: ([1, 1, 2, 2, 3, 3], 4)
Expected Output: [[1, 3], [2, 2]]
Explanation: 1+3 and 2+2 both equal 4; repeated 1/2/3 skipped to avoid duplicate pairs.
Input: ([1, 2, 3, 4, 5], 100)
Expected Output: []
Explanation: No pair sums to 100 (no-solution edge case).
Input: ([], 5)
Expected Output: []
Explanation: Empty array (edge case).
Input: ([-3, -1, 0, 2, 4, 6], 3)
Expected Output: [[-3, 6], [-1, 4]]
Explanation: -3+6=3 and -1+4=3; two distinct pairs with negatives.
Input: ([0, 0, 0, 0], 0)
Expected Output: [[0, 0]]
Explanation: All zeros; the single value-pair [0,0] is reported once, not C(4,2) times.
Input: ([-5, -3, -1, 2, 4], -4)
Expected Output: [[-3, -1]]
Explanation: -3 + -1 = -4 (negatives, single pair).
Hints
- Place one pointer at the start and one at the end. If the sum is too small, advance the left pointer; if too large, retreat the right pointer.
- When you find a match, record the value-pair, then skip over ALL further equal values on both sides before continuing — this is the deduplication step.
- Because the array is sorted, equal values are contiguous, so skipping them with while-loops keeps the scan O(n) overall and prevents duplicate pairs.