PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of data structures and streaming algorithms for maintaining k-th order statistics under dynamic inserts, emphasizing time-space trade-offs and correct handling of duplicates.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Maintain k-th largest in a stream

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Design and implement a class KthLargest that, given an integer k and an initial list of integers, supports: ( 1) KthLargest(k, nums): constructor; ( 2) add(val): adds val to the data structure and returns the current k-th largest element. Achieve O(log k) time per insertion and O(k) space. Describe your data structures, handle duplicates, and analyze complexity.

Quick Answer: This question evaluates understanding of data structures and streaming algorithms for maintaining k-th order statistics under dynamic inserts, emphasizing time-space trade-offs and correct handling of duplicates.

Initialize with nums, then after each added value return the current k-th largest element using a min-heap of size k.

Constraints

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

Examples

Input: (3, [4,5,8,2], [3,5,10,9,4])

Expected Output: [4, 5, 5, 8, 8]

Explanation: Classic sequence.

Input: (1, [], [2,1])

Expected Output: [2, 2]

Explanation: Largest so far.

Input: (3, [], [1,2])

Expected Output: [None, None]

Explanation: Fewer than k elements.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,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
  • 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 A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Build Prefix Lookup with a Trie - Google (medium)
  • Deterministic Task Execution Order - Google (easy)
  • Busiest Rental Car - Google (easy)
  • Find Common Free Time Slots Across Calendars - Google (easy)