Find the Closest Stored Timestamp in a BST

Last updated: November 29, 2026

Quick Overview

Story upload timestamps are stored in a binary search tree. Given a target time (which may be fractional), return the stored timestamp closest to it, preferring the smaller value on a tie.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Story upload timestamps are stored in a binary search tree. Given a target time (which may be fractional), return the stored timestamp closest to it, preferring the smaller value on a tie.

A quick BST navigation problem in the style of Meta's screens. The interviewer wants a single root-to-leaf walk with O(1) space and a clearly stated tie-break rule.

What the Interviewer Expects
  • Walks a single path using the BST property
  • Tracks the best candidate as it descends
  • States and implements the tie-break explicitly
  • Uses O(1) extra space
  • Discusses the k-closest generalization
Key Topics to Cover
Binary search tree
Binary search
Greedy descent
Tie-breaking
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
  • Return the k closest values instead of one.
  • How would you do this with a sorted array instead of a tree?
  • What if the tree is huge and stored on disk as a B-tree?
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

Tree is non-empty with distinct integer keys. Target is a float. If two values are equally close, return the smaller. Expected O(h) time and O(1) space....

High-Level Design

Start at the root with best = root.val. At each node, compare its distance to the current best and update (on a tie keep the smaller value). Then move left if the target is smaller than the node, el...


Submit Your Answer
Markdown supported