Markov Transition Counts and Probabilities
Reported by candidates from Retool's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Retool reportedly asked this one in March 2026, and the detail that trips people is the output format: current->next:count/total, with the fraction left unreduced. So 2/2 stays 2/2. It's a counting problem dressed up as Markov chains. You walk adjacent pairs, tally them in a hash map, then format and sort. Nothing exotic. But with up to 200000 tokens and a strict string format, small slips cost you the whole test. If you blank on the structure mid-assessment, StealthCoder runs invisibly as a safety net and gives you the solution in real time.
The problem
Given an observed token sequence, count every adjacent transition from a current token to its next token. Return one row for each observed transition in the exact form current->next:count/total, where total is the number of outgoing transitions observed from current. The fraction is intentionally not reduced. Sort rows first by current, then by next. Function markovTransitionStats(tokens: String[]) → String[] Examples Example 1 tokens = ["a","b","a","c","a","b"] return = ["a->b:2/3","a->c:1/3","b->a:1/1","c->a:1/1"] The three outgoing transitions from a go to b twice and c once. Example 2 tokens = ["x","x","x"] return = ["x->x:2/2"] Both adjacent pairs are self-transitions. Example 3 tokens = ["z","a"] return = ["z->a:1/1"] One pair creates one certain transition. Constraints 2 <= tokens.length <= 200000. Each token has 1 to 30 lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two counters. Walk i from 0 to n-2 and increment a nested map: pairCount[current][next], plus outTotal[current]. The total is the number of outgoing transitions from current, not the number of times current appears, so the last token never adds to a total. Then sort current keys, sort each next key, and emit current->next:count/total. Pitfalls: reducing the fraction (don't, 2/2 must stay 2/2), counting the final token as having an outgoing edge, and sorting by the formatted string instead of by the tokens. Sorting the strings can misorder things because of the arrow and digits. Use plain string comparison on the tokens. Complexity is O(n + k log k) where k is the number of distinct transitions. If your mind goes blank on the nested map, StealthCoder is the hedge during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Markov Transition Counts and Probabilities cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Retool's OA.
Retool reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Markov Transition Counts and Probabilities FAQ
How hard is the Markov transition problem really?+
Easy to medium. There's no clever algorithm, just a hash map of counts and careful formatting. Most failures come from the output string or the sort order, not the logic. If you can write a nested map and a sort, you can finish it.
What's the trick to getting the total right?+
The total is the count of outgoing transitions from current, which is the sum of its pair counts. Keep a separate counter incremented once per adjacent pair, keyed by the current token. The final token has no outgoing edge, so it never contributes.
Do I need to reduce the fractions?+
No. The statement says the fraction is intentionally not reduced. Example 2 returns x->x:2/2, not 1/1. Skip any gcd logic. Printing count and total directly is correct and simpler.
How should I sort the output rows?+
Sort by current token first, then by next token, both as plain lexicographic strings. Sort the map keys rather than the finished row strings, so the arrow and digits can't change the order. Tokens are lowercase letters, so default comparison works.
Will it run fast enough for 200000 tokens?+
Yes. One pass over the array with hash map updates is O(n), and sorting only the distinct transitions is cheap. Avoid string concatenation to build keys inside the loop if you can. Nested maps keep it clean and fast.