PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • Medium
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Solve array and tree algorithm challenges

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Solve the following three algorithmic tasks: 1) Unique triplet sum: Given an integer array nums and an integer target (default 0), return all unique triplets [a, b, c] such that a + b + c = target with no duplicate triplets. Explain an O(n^ 2) approach using sorting and two pointers, and detail how you handle duplicates and edge cases. 2) K most common values: Given an integer array nums and an integer k, return the k values with highest frequency. If two values share the same frequency, order them by value descending in the output. Implement an O(n log k) solution using a min-heap of size k (e.g., Python’s heapq) and explain how you produce final descending order. 3) Equalize root-to-leaf path sums with minimal increments: You are given a complete binary tree represented as an array costs where costs[i] is the nonnegative cost at node i. In one operation you may increment any node’s cost by 1. Compute the minimum total increments required so that every root-to-leaf path has the same sum. Provide an algorithm and analyze its time and space complexity.

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)

Given an integer array `nums` and an integer `target` (default `0`), return all unique triplets `[a, b, c]` such that `a + b + c == target`, with no duplicate triplets in the output. Sort the array, then for each index `i` run a two-pointer scan over the remaining suffix to find pairs summing to `target - nums[i]`. Skip equal neighbouring values at the fixed index and after recording a hit to avoid duplicate triplets. This yields an O(n^2) time, O(1) extra-space (ignoring output) solution. The order of triplets in the returned list follows the sorted-array scan (non-decreasing by first element, then second).

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.

Input: ([1, 2, 3], 100)

Expected Output: []

Explanation: No triplet reaches the target.

Input: ([], 0)

Expected Output: []

Explanation: Empty input returns an empty list.

Input: ([-2, 0, 1, 1, 2], 0)

Expected Output: [[-2, 0, 2], [-2, 1, 1]]

Explanation: Duplicate 1s produce the valid triplet [-2,1,1] exactly once.

Input: ([3, -2, 1, 0, -1, 2, -3], 1)

Expected Output: [[-3, 1, 3], [-2, 0, 3], [-2, 1, 2], [-1, 0, 2]]

Explanation: Non-zero target; four unique triplets sum to 1, listed in sorted-scan order.

Hints

  1. Sort the array first so equal values are adjacent — this makes both duplicate-skipping and the two-pointer scan possible.
  2. 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.
  3. 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

Given an integer array `nums` and an integer `k`, return the `k` values with the highest frequency. When two values share the same frequency, the one with the **larger value** comes first. The final result is ordered by frequency descending, then by value descending. Use a min-heap of size `k` keyed by `(frequency, value)`: push each distinct value's `(count, value)` pair and pop whenever the heap exceeds `k`. Because the smallest `(freq, value)` is evicted, the heap retains the k highest-frequency values, breaking frequency ties in favour of the larger value. Finally sort the surviving k entries by `(freq, value)` descending to produce the output. This is O(n log k) time.

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.

Input: ([4, 4, 5, 5, 6], 2)

Expected Output: [5, 4]

Explanation: 4 and 5 tie at frequency 2; the tie is broken by value descending so 5 comes first.

Input: ([7, 7, 8, 8, 9, 9], 3)

Expected Output: [9, 8, 7]

Explanation: All three values tie at frequency 2, so output is purely value descending.

Input: ([], 3)

Expected Output: []

Explanation: Empty input returns empty.

Input: ([10, 20, 30], 0)

Expected Output: []

Explanation: k = 0 returns nothing.

Input: ([-1, -1, -2, -2, -3], 2)

Expected Output: [-1, -2]

Explanation: -1 and -2 tie at frequency 2; -1 > -2 so it comes first.

Input: ([5, 5, 5, 1, 1, 9], 2)

Expected Output: [5, 1]

Explanation: 5 (freq 3) and 1 (freq 2) win; 9 (freq 1) is excluded.

Hints

  1. Count frequencies first with a hash map / Counter.
  2. 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.
  3. 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

You are given a complete (perfect) binary tree stored as a heap array `costs`, where `costs[i]` is the nonnegative cost at node `i`, node `i`'s children are at indices `2*i + 1` and `2*i + 2`. In one operation you may increment any single node's cost by 1. Return the **minimum total number of increments** so that every root-to-leaf path has the same sum. Process the tree bottom-up. For each internal node, its two child subtrees must have equal post-equalization path sums, so you raise the cheaper child's required sum up to the more expensive one; the gap `(max - left) + (max - right)` is added to the running total. The node's own required path sum becomes `costs[i] + max(left, right)`. Leaves contribute their own cost as their path sum. This greedy bottom-up pass is optimal because increments are never wasted — you only ever lift the lighter side to match the heavier, which is forced.

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.

Input: ([5],)

Expected Output: 0

Explanation: Single node is the only path; nothing to equalize.

Input: ([1, 1, 1],)

Expected Output: 0

Explanation: Both leaves already equal.

Input: ([1, 2, 3],)

Expected Output: 1

Explanation: Leaves 2 and 3 differ by 1; raise the 2-leaf to 3.

Input: ([10, 1, 5, 0, 0, 0, 0],)

Expected Output: 4

Explanation: Each child subtree is internally balanced (leaves all 0); root must lift the left child path (1) to match the right child path (5), costing 4.

Input: ([],)

Expected Output: 0

Explanation: Empty tree needs no increments.

Input: ([0, 0, 0, 1, 2, 3, 4],)

Expected Output: 4

Explanation: Left subtree: lift leaf 1 to 2 (cost 1). Right subtree: lift leaf 3 to 4 (cost 1). Then root lifts left path (2) to right path (4) (cost 2). Total 4.

Hints

  1. Think bottom-up: a node's two subtrees must end up with equal path sums before you can reason about the node above it.
  2. 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.
  3. 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.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)