PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of binary search tree traversal and iterator design with amortized time and space analysis, as well as dynamic priority-queue design for maintaining ordered tickets with severity, recency, and tie-breaking rules.

  • medium
  • Meta
  • Coding & Algorithms
  • Machine Learning Engineer

Implement BST Iterator and Ticket Queue

Company: Meta

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

The coding interviews mentioned two algorithm/data-structure tasks: 1. **Implement a binary search tree iterator.** You are given the root of a binary search tree. Design an iterator that supports: - `hasNext()` -> returns whether there is another value to visit - `next()` -> returns the next smallest value in the tree The iterator must return values in ascending order. Aim for `O(h)` extra space, where `h` is the height of the tree, and amortized `O(1)` time per operation. 2. **Maintain a prioritized ticket queue with updates.** Build a data structure for support tickets. Each ticket has: - `ticket_id`: unique integer - `severity`: one of `low`, `medium`, or `high` - `t`: integer timestamp, where larger means more recent Support the following operations efficiently: - `upsert(ticket_id, severity, t)`: insert a new ticket or update an existing ticket - `get_top_ticket()`: return the highest-priority ticket Priority rules: 1. Higher severity comes first (`high > medium > low`) 2. If severity is the same, the more recent ticket (`t` larger) comes first 3. If there is still a tie, return the smaller `ticket_id` Return `-1` if no tickets exist.

Quick Answer: This question evaluates understanding of binary search tree traversal and iterator design with amortized time and space analysis, as well as dynamic priority-queue design for maintaining ordered tickets with severity, recency, and tie-breaking rules.

BST Iterator Operation Simulation

Given BST level-order values and iterator operations, return outputs for next and hasNext in operation order.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([7,3,15,None,None,9,20], ["next","next","hasNext","next","hasNext","next","hasNext"] )

Expected Output: [3, 7, True, 9, True, 15, True]

Explanation: Iterator visits values in ascending order.

Input: ([], ["hasNext"])

Expected Output: [False]

Explanation: Empty tree.

Input: ([2,1,3], ["hasNext","next","next","next","hasNext"])

Expected Output: [True, 1, 2, 3, False]

Explanation: Full traversal.

Hints

  1. Pick a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.

Prioritized Ticket Queue with Updates

Process upsert and get_top_ticket operations. Priority is severity, then newer timestamp, then smaller ticket id.

Constraints

  • Inputs are Python literals matching the function signature.
  • Return a deterministic exact-match value.

Examples

Input: ([['get_top_ticket'], ['upsert', 10, 'low', 5], ['upsert', 2, 'high', 3], ['get_top_ticket'], ['upsert', 10, 'high', 9], ['get_top_ticket']],)

Expected Output: [-1, None, None, 2, None, 10]

Explanation: Updates change priority.

Input: ([['upsert', 2, 'medium', 7], ['upsert', 1, 'medium', 7], ['get_top_ticket']],)

Expected Output: [None, None, 1]

Explanation: Tie breaks by smaller id.

Hints

  1. Pick a representation that makes the requested operation direct.
  2. Handle empty inputs and boundary cases first.
Last updated: Jun 27, 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

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)