First Promise to Settle
Updated
Problem statement
Write firstSettled(items). items is an array. Each item is a promise or any other value. Return a new promise that settles the same way as the first item to settle: if that item fulfills with a value, so does the new promise, and if it rejects with a reason, so does the new promise. Do not use Promise.race (the tests cannot check this; the point is to build it yourself).
Rules for this exercise:
- An item that is not a promise counts as ready at once with that value, and an object with a
thenmethod counts as a promise. - Plain values and promises that are already settled when you get them are taken in array order: the first of them wins.
- Items that settle later are ignored, and their results never change the new promise.
- If
itemsis empty, the new promise never settles.
How the tests are written
Each test is a short script: a list of steps that run in order. {"let": "x", "value": V} makes a value called x. A value written as {"$js": "expression"} is the result of that JavaScript expression, and the expression may use names made earlier. {"call": "f", "args": [...]} calls f. The name solution is the function you write. A name like "obj.method" calls the method with obj as this. An optional "this" sets this for the call, an optional "new": true calls it with new, and an optional "let" saves the result under a name.
The expected output has one entry for each call, await and state step, in order: {"returns": value} or {"throws": "ErrorName"}. A function is written {"$function": true} and undefined is written {"$undefined": true}.
Tests use fake time. resolveAfter(ms, value) makes a promise that fulfills with value after ms time units, and rejectAfter(ms, reason) makes one that rejects. {"tick": ms} moves the clock forward. {"await": "p"} moves the clock until p settles and records {"resolves": value, "at": time} or {"rejects": reason, "at": time}, or {"pending": true, "at": time} if it never settles. {"state": "p"} records the state without moving the clock: {"pending": true}, {"resolved": value} or {"rejected": reason}. A promise is written {"$promise": true} and an error {"$error": "Error", "message": "..."}. Each test starts at time 0 and runs at once.
Function
firstSettled(steps: JsSteps) → JsStepResultsExamples
Example 1
steps = [{"let":"tea","value":{"$js":"resolveAfter(80, 'tea')"}},{"let":"coffee","value":{"$js":"resolveAfter(30, 'coffee')"}},{"call":"solution","args":[[{"$js":"tea"},{"$js":"coffee"}]],"let":"r"},{"state":"r"},{"tick":29},{"state":"r"},{"await":"r"}]return = [{"returns":{"$promise":true}},{"pending":true},{"pending":true},{"resolves":"coffee","at":30}]teafulfills at time 80 andcoffeeat time 30.firstSettledreturns a promiser.- Right away,
ris pending. At time 29 it is still pending. - Waiting for
rtakes the clock to 30, whencoffeefulfills:rfulfills with"coffee"at 30. The laterteais ignored.
Example 2
steps = [{"let":"slow","value":{"$js":"resolveAfter(60, 'ok')"}},{"let":"bad","value":{"$js":"rejectAfter(20, new Error('offline'))"}},{"call":"solution","args":[[{"$js":"slow"},{"$js":"bad"}]],"let":"r"},{"await":"r"}]return = [{"returns":{"$promise":true}},{"rejects":{"$error":"Error","message":"offline"},"at":20}]slowwould fulfill at 60, butbadrejects at 20 with the erroroffline.- The first to settle is
bad, sorrejects with that error at 20.
Example 3
steps = [{"let":"slow","value":{"$js":"resolveAfter(30, 'a')"}},{"call":"solution","args":[[{"$js":"slow"},5]],"let":"r"},{"await":"r"}]return = [{"returns":{"$promise":true}},{"resolves":5,"at":0}]- The first item is a promise that would fulfill at 30. The second item is the plain number 5, which is ready at once.
- So
rfulfills with 5 at time 0.
Constraints
- items has 0 to 5 entries
- Each test has 2 to 7 steps, and times are whole numbers from 0 to 1000
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Rank-Based Cache with a Data SourceLinkedIn · PHONE SCREENLinkedIn interview questions
- Concurrent Token-Bucket Rate LimiterCharta Health · PHONE SCREENCharta Health interview questions
- Ordered Number Printing with Three ThreadsArize AI · PHONE SCREEN
- Bounded Service Instance RegistryRevolut · PHONE SCREENRevolut interview questions
- Modal Lock and Fair Modal LockOpenAI · PHONE SCREENOpenAI interview questions
- Course Ordering with Concurrent UpdatesNuro · PHONE SCREENNuro interview questions
- Single-Flight Chunk CacheModal Learning · PHONE SCREEN
- Thread-Safe Request Counts by AttributeRubrik · ONSITE INTERVIEWRubrik interview questions
- Thread-Safe Concurrent Linked ListApple · ONSITE INTERVIEWApple interview questions
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)