Ordered Odd-Even Thread Output
Updated
Problem statement
Coordinate two workers over the integers from 1 through n. The odd worker may append only odd values and the even worker may append only even values. The shared output must be exactly [1, 2, ..., n].
Return the shared output after both workers finish. Use synchronization so a worker waits until the next value has its parity; do not generate two independent lists and merge them afterward.
Function
orderedOddEvenOutput(n: int) → int[]Examples
Example 1
n = 6return = [1,2,3,4,5,6]The odd and even workers alternate while preserving increasing order.
Example 2
n = 1return = [1]Only the odd worker appends a value.
Constraints
0 <= n <= 100000- The returned array is empty when
n = 0.
More Goldman Sachs Onsite Interview questions
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Dungeon HealthONSITE INTERVIEW · Seen Sep 2026
- Insert, Delete, And Get Random In Constant TimeONSITE INTERVIEW · Seen Sep 2026
- Least Recently Used CacheONSITE INTERVIEW · Seen Sep 2026
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Concurrent Buffered File LoggerDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Bounded Producer–Consumer QueueMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
- Thread-Safe Task Storage and Worker DispatchNuro · ONSITE INTERVIEWNuro interview questions
- Shared Counter Snapshot SumTesla · ONSITE INTERVIEWTesla interview questions
- Circuit Breaker State MachineWise · ONSITE INTERVIEW
- One-Shot Callback Event DispatchPure Storage · ONSITE INTERVIEWPure Storage interview questions
- Blocking Connection Pool Operation TraceGoogle · ONSITE INTERVIEWGoogle interview questions
- Keyed Bounded Executor ScheduleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Concurrent Web Crawler SimulationOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Concurrent Discounted Card PurchasesBrex · PHONE SCREEN