Design a Lazy Union Iterator for Sorted Inputs

Last updated: August 26, 2026

Quick Overview

The question asks the candidate to design a Lazy Union Iterator that merges two sorted iterators into a single sorted iterator using lazy evaluation.

MongoDB
Software Engineering Fundamentals
Software Engineer
MongoDB
August 26, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

2

0

42 solved


The question asks the candidate to design a Lazy Union Iterator that merges two sorted iterators into a single sorted iterator using lazy evaluation.

This question is typically asked in the technical screen round to assess a candidate's understanding of data structures and algorithms, particularly in the context of merging sorted data. The interviewer is evaluating the candidate's ability to implement efficient algorithms while adhering to constraints like lazy evaluation, which is crucial for performance in large datasets.

What the Interviewer Expects
  • Explain the concept of lazy evaluation and its importance in this context.
  • Clearly outline the two primary functions: has_next() and next().
  • Demonstrate an understanding of how to maintain the sorted order while merging.
  • Discuss potential edge cases and how to handle them.
  • Provide a clear and efficient implementation in code.
Key Topics to Cover
Lazy Evaluation
Iterators
Merging Algorithms
Data Structures
Complexity Analysis
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you extend this to merge K sorted iterators efficiently?
  • What are the time and space complexities of your solution?
  • Can you discuss any trade-offs in your design?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

The goal is to create a Lazy Union Iterator that takes two sorted iterators as input and merges them into a single sorted iterator. The iterator must implement two functions: has_next() to check i...

High-Level Design

The Lazy Union Iterator will maintain pointers to the current elements of both input iterators. The has_next() function will check if either iterator has more elements, while the next() functi...


Submit Your Answer
Markdown supported

Related Questions