Solve array and tree algorithm challenges
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving skills across array, heap, and tree data structures, testing competencies in duplicate handling, frequency aggregation, and balancing root-to-leaf path sums within the Coding & Algorithms domain.
Unique Triplet Sum (3Sum)
Constraints
- 0 <= len(nums) <= 3000
- -10^5 <= nums[i] <= 10^5
- target defaults to 0 if not provided
- Each triplet in the output must be unique (no duplicate [a, b, c] sets)
Examples
Input: ([-1, 0, 1, 2, -1, -4], 0)
Expected Output: [[-1, -1, 2], [-1, 0, 1]]
Explanation: Two distinct triplets sum to 0; the duplicate -1 is handled so [-1,0,1] appears once.
Input: ([0, 0, 0, 0], 0)
Expected Output: [[0, 0, 0]]
Explanation: All zeros yield a single unique triplet despite four zeros.
Hints
- Sort the array first so equal values are adjacent — this makes both duplicate-skipping and the two-pointer scan possible.
- Fix the first element with an outer loop, then use a left/right two-pointer scan on the remaining suffix to find the other two values.
- After fixing index i, skip it when nums[i] == nums[i-1]; after recording a hit, advance past equal lo/hi values to avoid duplicate triplets.
K Most Common Values
Constraints
- 0 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- 0 <= k <= number of distinct values in nums
- Frequency ties are broken by value descending; final output is sorted by frequency descending, then value descending
Examples
Input: ([1, 1, 1, 2, 2, 3], 2)
Expected Output: [1, 2]
Explanation: 1 (freq 3) then 2 (freq 2) are the two most common.
Input: ([1], 1)
Expected Output: [1]
Explanation: Single element.
Hints
- Count frequencies first with a hash map / Counter.
- Keying the size-k min-heap by the tuple (frequency, value) lets a single pop evict the worst candidate AND break frequency ties toward the smaller value.
- A min-heap pops smallest-first, so to get descending output you sort the surviving k entries by (freq, value) in reverse at the end.
Equalize Root-to-Leaf Path Sums with Minimal Increments
Constraints
- 0 <= len(costs); the tree is a complete (perfect) binary tree, so len(costs) is typically 2^h - 1
- 0 <= costs[i] <= 10^4
- Only increments (cost + 1) are allowed; you may never decrease a cost
- Goal: all root-to-leaf path sums become equal with the fewest total increments
Examples
Input: ([0, 0, 0, 0, 0, 0, 0],)
Expected Output: 0
Explanation: All zeros, all paths already sum to 0.
Input: ([0, 0, 0, 0, 0, 1, 1],)
Expected Output: 1
Explanation: Right subtree's leaves are both 1 (already equal); root must lift its left child path (0) to match the right child path (1), costing 1.
Hints
- Think bottom-up: a node's two subtrees must end up with equal path sums before you can reason about the node above it.
- At each internal node the only optimal move is to raise the lighter child subtree's path sum up to the heavier one — the gap (max - left) + (max - right) is the increments charged at that node.
- Carry up costs[i] + max(left_child_sum, right_child_sum) as the node's own equalized path sum, and accumulate all the gaps as you ascend.