FastPrepLazy K-Way Union Iterator
Problems/Lazy K-Way Union Iterator

Lazy K-Way Union Iterator

MongoDB logoMongoDB● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in MongoDB phone screens for a full-time role. The reports describe a lazy union of two iterators with unique sorted output, examples with integers and text, and a follow-up with K iterators. We chose the single value type per run, the string encoding, a finite sequence of calls, what happens after the iterators run out, and the limits.

Our version is about 95% the same as the reported questions.

Build a lazy union iterator. It reads from several input iterators, and each input gives its values in strictly increasing order. The union returns every distinct value once, in strictly increasing order. Lazy means it pulls values only when asked and never builds the whole merged list.

The first version of the task has two input iterators. The follow-up allows k of them.

Each row of sortedIterators holds one input iterator's values, written as strings. valueType tells you what the values are. If it is int, every value is an integer written in plain decimal, and you compare values as numbers. If it is string, every value is text, and you compare values in dictionary order.

Run the calls in operations in order, all on one union iterator that keeps its state between calls:

  • has_next returns true if a distinct value is left, otherwise false. It must not use up any value.
  • next returns the smallest value left. If several input iterators have that same value at their front, return it once and move all of them forward.
  • Calling next when nothing is left returns ERROR_EXHAUSTED and changes nothing.

Return one string per call, in order. Write integer results in plain decimal. Keep at most one current front value per input iterator. Extra memory is O(1) for two iterators and O(k) for the follow-up, not counting the input and the output. Do not sort or flatten the whole input.

Complete runUnionIterator. The rows of strings and the list of calls stand in for a real iterator of integers or strings. Your iterator must still be lazy.

Function

runUnionIterator(sortedIterators: String[][], operations: String[], valueType: String) → String[]

Examples

Example 1

sortedIterators = [["2","4","6","8","10"],["1","2","3","5","8","13"]]operations = ["next","next","next","next","next","next","next","next","next"]valueType = "int"return = ["1","2","3","4","5","6","8","10","13"]

The union is 1,2,3,4,5,6,8,10,13. Shared values 2 and 8 each appear once.

Example 2

sortedIterators = [["apple","banana","orange","tree"],["apple","tree","zoo"]]operations = ["next","next","next","next","next"]valueType = "string"return = ["apple","banana","orange","tree","zoo"]

Both rows contain apple and tree, and each is returned once. The text union is apple,banana,orange,tree,zoo.

Example 3

sortedIterators = [["-10","-9","-5","-1","0","2","4"],["-9","-5","-3","-2","0","1","2"]]operations = ["next","next","next","next","next","next","next","next","next","next"]valueType = "int"return = ["-10","-9","-5","-3","-2","-1","0","1","2","4"]

Numeric comparison handles negative values. The result is -10,-9,-5,-3,-2,-1,0,1,2,4.

Example 4

sortedIterators = [["1","2","3"],[]]operations = ["next","next","next"]valueType = "int"return = ["1","2","3"]

The second iterator is empty, so it gives no values. The first iterator produces 1,2,3.

Constraints

  • 0 <= sortedIterators.length <= 200, with at most 200000 values across all rows.
  • 0 <= operations.length <= 2000.
  • Every row is strictly increasing under the chosen comparison. The same value may appear in different rows.
  • valueType is int or string; all values share that type.
  • Integers are written in base ten with no leading plus sign and no leading zeros, except 0 itself. They lie within [-2147483648,2147483647].
  • Text values have 1 through 32 lowercase ASCII letters. They are compared letter by letter, and a value that is a shorter prefix of another comes first.
  • Each operation is has_next or next. Calling either one again, or calling next after the end, is allowed.
  • Your solution must stay lazy and keep within the extra-memory limits above.

More MongoDB interview questions

  • String Interpolation with a Missing-Value ModePHONE SCREEN · Seen Nov 2026
  • Build Packages as Dependencies Are DiscoveredPHONE SCREEN · Seen Oct 2026
  • Finite Web CrawlerPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Recursive Nested JSON Key SearchPHONE SCREEN · Seen Aug 2026
  • Predicate Expression Tree EvaluatorPHONE SCREEN · Seen Aug 2026
  • JSON Path Lookup with Implicit Array WildcardsONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Paged KV Cache AccessNVIDIA · PHONE SCREENNVIDIA interview questions
  • Alert Engine with a Repeat IntervalChronosphere · PHONE SCREEN
  • Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
  • Key-Value Store on a Block DeviceLambda · PHONE SCREEN
  • Implement a Heap Priority QueueByteDance · PHONE SCREENByteDance interview questions
  • Design Authentication ManagerApple · PHONE SCREENApple interview questions
  • LLM Provider Pool FailoverPerplexity AI · PHONE SCREENPerplexity AI interview questions
  • Periodic Event LoopEtched · PHONE SCREEN
  • ID Allocator with Assign and ReleaseSnap Inc. · PHONE SCREENSnap Inc. interview questions
  • 15-Minute Chat Session TrackerOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions

MongoDB interview experiences

  • MongoDB Experienced SWE Interviews (Atlas Growth): 1 Experience
All MongoDB interview questionsSee MongoDB hiring insights
Saved
class Solution {
    public String[] runUnionIterator(String[][] sortedIterators, String[] operations, String valueType) {
        // Write your code here.
    }
}
Checking account…