PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates competence in streaming algorithms and resource-constrained algorithm design, specifically implementing a k-way merge over blocking, potentially unbounded integer iterators while preserving stable duplicates and reasoning about time and O(k) space complexity.

  • Medium
  • Amazon
  • Coding & Algorithms
  • Data Scientist

Implement streaming k-way merge with constraints

Company: Amazon

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

Implement a function merge_k(iterators, N) that returns the first N items of the global ascending order from k sorted, potentially unbounded iterators of integers. Constraints: iterators may block, memory must be O(k), and duplicates must be preserved but made stable by source index (stable tie-breaker). Specify time complexity, show how you would handle iterator exhaustion, and discuss how to add deduplication (unique-only) without increasing asymptotic complexity. Provide unit tests that cover k=1, empty streams, large N, and pathological inputs (e.g., one iterator far slower than the rest).

Quick Answer: This question evaluates competence in streaming algorithms and resource-constrained algorithm design, specifically implementing a k-way merge over blocking, potentially unbounded integer iterators while preserving stable duplicates and reasoning about time and O(k) space complexity.

Given finite sorted streams, return the first N globally sorted items preserving duplicates and breaking ties by source index.

Constraints

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

Examples

Input: ([[1,4,7],[1,3,5],[2,6]], 6)

Expected Output: [1, 1, 2, 3, 4, 5]

Explanation: Tie broken by source index while preserving duplicates.

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

Expected Output: [2, 3]

Explanation: Empty stream.

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

Expected Output: [1, 2]

Explanation: Single stream.

Hints

  1. Choose 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
  • 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

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)