PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/LinkedIn

Find k closest values in a BST

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's understanding of binary search tree properties, iterator-based traversal (predecessor/successor), and algorithmic complexity reasoning by requiring selection of k values nearest to a real target under O(log n + k) expected time and O(h) extra space constraints.

  • Medium
  • LinkedIn
  • Coding & Algorithms
  • Software Engineer

Find k closest values in a BST

Company: LinkedIn

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a binary search tree with n nodes and a real target t, return k node values whose distances to t are smallest. Implement an algorithm with O(log n + k) expected time and O(h) extra space (h is tree height), using predecessor and successor iterators (e.g., two in-order stacks) or a recursive approach. Specify tie-breaking rules, handle k > n and duplicate values, and analyze correctness and complexity. Discuss how you would adapt the solution if the structure were a general binary tree (not a BST).

Quick Answer: This question evaluates a candidate's understanding of binary search tree properties, iterator-based traversal (predecessor/successor), and algorithmic complexity reasoning by requiring selection of k values nearest to a real target under O(log n + k) expected time and O(h) extra space constraints.

Related Interview Questions

  • Count Trips From Vehicle Logs - LinkedIn (easy)
  • Design O(1) Randomized Multiset - LinkedIn (easy)
  • Process Mutable Matrix Sum Queries - LinkedIn (medium)
  • Design a Randomized Multiset - LinkedIn (medium)
  • Can You Place N Objects? - LinkedIn (medium)
LinkedIn logo
LinkedIn
Aug 10, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0

Given a binary search tree with n nodes and a real target t, return k node values whose distances to t are smallest. Implement an algorithm with O(log n + k) expected time and O(h) extra space (h is tree height), using predecessor and successor iterators (e.g., two in-order stacks) or a recursive approach. Specify tie-breaking rules, handle k > n and duplicate values, and analyze correctness and complexity. Discuss how you would adapt the solution if the structure were a general binary tree (not a BST).

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More LinkedIn•More Software Engineer•LinkedIn Software Engineer•LinkedIn Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.