Flatten 2D Vector
Updated
Problem statement
Design an iterator over a 2D array vec whose rows can have different lengths, including length zero. The iterator returns the values row by row, and from left to right inside each row. It supports two calls:
nextreturns the next value and moves past it.hasNextreturns whether another value is left. It does not move the iterator, so calling it many times in a row gives the same answer.
The calls are given in order in operations, and the iterator starts before the first value of vec. Return one string for each call: the value as a decimal number for next, and "true" or "false" for hasNext.
Function
runVector2D(vec: int[][], operations: String[]) → String[]Examples
Example 1
vec = [[1,2],[3],[4]]operations = ["next","next","next","hasNext","hasNext","next","hasNext"]return = ["1","2","3","true","true","4","false"]The values come out row by row: 1 and 2 from the first row, then 3, then 4. Calling hasNext twice in a row does not use up 4. After 4, nothing is left, so the last hasNext returns "false".
Example 2
vec = [[],[5],[],[],[6,7],[]]operations = ["hasNext","next","next","hasNext","next","hasNext"]return = ["true","5","6","true","7","false"]Empty rows hold no values, so they are skipped. The first next returns 5. The second skips the two empty rows after it and returns 6. After 7, only an empty row is left, so hasNext returns "false".
Example 3
vec = [[],[]]operations = ["hasNext","hasNext"]return = ["false","false"]Both rows are empty, so there is never a next value.
Constraints
0 <= vec.length <= 2000 <= vec[i].length <= 500-500 <= vec[i][j] <= 5001 <= operations.length <= 3000- Each
operations[i]is"next"or"hasNext". nextis called only when another value is left.
More Airbnb Phone Screen questions
- Read Across Sound TracksPHONE SCREEN · Seen Nov 2026
- Text JustificationPHONE SCREEN · Seen Nov 2026
- Decode and Store StringsPHONE SCREEN · Seen Oct 2026
- Print Sentences as TablePHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Detonate Bombs with Chain ReactionsPHONE SCREEN · Seen Aug 2026
- Smallest Common RegionPHONE SCREEN · Seen Aug 2026
- Longest Chain BookingPHONE SCREEN · Seen Aug 2026
- Nested List Iterator with RemovePHONE SCREEN · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Pacific Atlantic Water FlowPHONE SCREEN · Seen May 2023
Similar questions at other companies
- Sparse Vector Dot ProductAnduril · PHONE SCREENAnduril interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREENWhatNot interview questions
- Interval List IntersectionsGrammarly · PHONE SCREEN
- Customer Bootstrap APIDoorDash · PHONE SCREEN · ONSITE INTERVIEWDoorDash interview questions
- Design Hit CounterCharta Health · PHONE SCREENCharta Health interview questions
- Subarray Product Less Than KApple · PHONE SCREENApple interview questions
- 3SumAmazon · PHONE SCREEN
- Subarray Product Less Than KAngel One · PHONE SCREEN
- Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
Interview reports that mention this question
Airbnb interview experiences
- Airbnb SWE OA: Four-Level Banking System: 1 Experience
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)