Character Counts with Retried Remote Workers
Updated
Problem statement
Implement a manager that counts characters using workers whose construction or result retrieval may fail. You are given text, a positive workerCount, and one faultScripts[i] string for each worker.
Worker i owns the characters at indices i, i + workerCount, i + 2 * workerCount, .... Preserve character case and spaces. Process workers in increasing index order, completing one worker before starting the next. Empty chunks still use a worker.
Deterministic failure schedules
For this exercise, a supplied script replaces random failures. Its characters describe consecutive attempts, starting at attempt 0. Each script consists of zero or more C or R characters followed by exactly one final S.
C: construction fails. Do not call the result method on this attempt.R: construction succeeds, then result retrieval fails.S: construction and result retrieval both succeed; the result contains the character counts for that worker's chunk.
After either failure, retry the same chunk by constructing a fresh worker. Aggregate only its one successful result, exactly once. Do not create any workers in advance and do not perform real network calls or random sampling.
Return value
Return a string matrix recording the simulated calls and counts. All indices and counts are decimal strings.
- For every attempt append
["construct", workerIndex, attemptIndex, "fail" or "ok"]. - If construction succeeds, append
["result", workerIndex, attemptIndex, "fail" or "ok"]. - Immediately after a successful result, append one
["worker-count", workerIndex, character, count]row for every character with a positive count in that chunk, ordered by ascending ASCII code. Then move to the next worker. - After all workers finish, append one
["count", character, count]row for every positive total count, also in ascending ASCII order.
A character field is the one-character string itself, including " " for a space. Zero counts produce no rows. The audit rows make retry behavior observable; there is no scheduling or optimization objective.
Interpretation
For example, Hello there contains one r, so the total count for r is "1". Workers are constructed inside the retry loop, not eagerly.
Function
countWithRetries(text: String, workerCount: int, faultScripts: String[]) → String[][]Examples
Example 1
text = "ab"workerCount = 2faultScripts = ["CS","RS"]return = [["construct","0","0","fail"],["construct","0","1","ok"],["result","0","1","ok"],["worker-count","0","a","1"],["construct","1","0","ok"],["result","1","0","fail"],["construct","1","1","ok"],["result","1","1","ok"],["worker-count","1","b","1"],["count","a","1"],["count","b","1"]]Worker 0 retries construction without making a result call; worker 1 retries after a failed result. Each successful worker contributes its one-character chunk exactly once.
Example 2
text = "aba"workerCount = 2faultScripts = ["S","S"]return = [["construct","0","0","ok"],["result","0","0","ok"],["worker-count","0","a","2"],["construct","1","0","ok"],["result","1","0","ok"],["worker-count","1","b","1"],["count","a","2"],["count","b","1"]]Round-robin chunks are aa and b, so the two worker-count groups differ. Final counts are a:2 and b:1.
Example 3
text = ""workerCount = 1faultScripts = ["RCS"]return = [["construct","0","0","ok"],["result","0","0","fail"],["construct","0","1","fail"],["construct","0","2","ok"],["result","0","2","ok"]]Even an empty chunk follows its failure script. There are no worker-count or final count rows.
Constraints
0 <= text.length <= 10000.textcontains only English letters and ASCII spaces.1 <= workerCount <= 100.faultScripts.length == workerCount.- Each script matches zero or more
C/Rcharacters followed by oneS; the total script length is at most1000.
More Coalition interview questions
Similar questions at other companies
- Business Account KYC Verification — Parts 1–5Stripe · PHONE SCREENStripe interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- String Interpolation with a Missing-Value ModeMongoDB · PHONE SCREENMongoDB interview questions
- Dasher Pay with Store Waits and Peak HoursDoorDash · PHONE SCREENDoorDash interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Fixed-Order Task Run Time With CooldownWaymo · PHONE SCREENWaymo interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Risk-Priority Detection StoreCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Environment Variable Store and SubstitutionAmazon · PHONE SCREEN