Kth Shortest Video Duration Stored in a Binary Search Tree

Last updated: November 29, 2026

Quick Overview

A creator's draft videos are indexed in a binary search tree keyed by duration in seconds (durations are unique). Return the kth shortest duration without converting the whole tree to a list first.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A creator's draft videos are indexed in a binary search tree keyed by duration in seconds (durations are unique). Return the kth shortest duration without converting the whole tree to a list first.

This is a short warm-up in TikTok phone screens that checks you understand BST ordering and can stop an in-order traversal early. Interviewers often follow up on how to make repeated kth queries fast.

What the Interviewer Expects
  • Uses in-order traversal because it visits keys in sorted order
  • Stops as soon as the kth node is visited
  • Implements the traversal iteratively with a stack
  • Handles k larger than the tree size
  • Explains the subtree-size augmentation for repeated queries
Key Topics to Cover
BST
in-order traversal
iterative traversal
order statistics
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • If the tree changes often and you need kth queries frequently, how would you augment it?
  • How would you find the kth longest instead?
  • What if durations are not unique?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Node: key, left, right.
  • Input: root, integer k (1-based).
  • Output: kth smallest key, or None if the tree has fewer than k nodes....
High-Level Design

In-order traversal of a BST yields keys in ascending order, so the kth visited node is the answer. An iterative traversal with a stack lets me stop immediately after k visits, costing O(h + k) instead...


Submit Your Answer
Markdown supported