Katkat

Solve array and tree algorithm challenges

TikTokSoftware Engineer · Technical Screen

Last updated

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.

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.
  3. 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.
  4. 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.