Reported September 2024
ZipRecruiterdynamic programming

Recursively Expanded Shell Command Counts

Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live ZipRecruiter OA. Under 2s to a working solution.
Founder's read

The ZipRecruiter OA reported in September 2024 looks like a toy shell simulator until you see history.length can hit 100000. Then naive recursion stops being cute. Each entry is cp, ls, mv, or a reference like !3 pointing at an earlier line, and you need total executions per command. If you blank on the trick, StealthCoder is the invisible safety net that reads the problem and hands you a clean solution mid-assessment. But the trick is small. Once you see it, this is a ten-line job.

The problem

Every history entry is cp, ls, mv, or !index, where index is one-based from the session start and refers to an earlier entry.
A reference recursively executes the referenced entry. Return counts in [cp, ls, mv] order, counting direct and referenced executions.

Function
countShellCommands(history: String[]) → int[]

Examples
Example 1
history = ["ls","cp","!1","!3","mv"]
return = [1,3,1]
Entries 1, 3, and 4 all execute ls.
Example 2
history = ["cp","!1","!2"]
return = [3,0,0]
Both references recursively resolve to cp.

Constraints
1 <= history.length <= 100000

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is memoized counts per entry. Since every reference points to an earlier index, process the history left to right and store a 3-element count vector for each entry. For cp, ls, or mv, the vector is a unit vector. For !k, copy the vector at index k. Add each vector to a running total. That's O(n) time and O(n) space. The pitfall is actually recursing on each reference. A chain like !1, !2, !3 and so on, repeated, can make you re-walk long chains, and deep recursion can overflow the stack at 100000 entries. Also watch the one-based indexing, it's an easy off-by-one. Check Example 1: entry 4 is !3, which copies entry 3, which is !1, which is ls. So ls totals 3. If the live OA freezes you, StealthCoder can supply this DP outline and code while you keep your head clear.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Recursively Expanded Shell Command Counts 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass ZipRecruiter's OA.

ZipRecruiter reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Recursively Expanded Shell Command Counts FAQ

What's the trick in the ZipRecruiter shell command problem?+

Store a [cp, ls, mv] count vector per history entry. A reference just copies the vector of the earlier entry it points to. Add each entry's vector to a running total. No recursion needed, because references always point backward.

Why does brute force fail here?+

With up to 100000 entries, resolving each reference by recursively walking the chain can repeat work massively and blow the call stack. Memoizing per index turns every reference into an O(1) lookup, so the whole thing runs in linear time.

Is this a dynamic programming problem?+

Yes, in a light form. Each entry's result depends only on an earlier entry's result. You fill an array in order and reuse values. It's the same idea as memoization, just done bottom-up with a simple loop over the history.

What edge cases should I test?+

Test a single entry, a long chain of references like !1, !2, !3, and references to references as in Example 1. Check one-based indexing carefully. Also confirm the output order is cp, ls, mv, not the order commands first appear.

How do I prepare for this in 48 hours?+

Write this solution from scratch twice. Then do two or three problems where each item depends on an earlier item, like climbing stairs or prefix computations. Focus on spotting when recursion can be replaced by a forward pass storing results.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with ZipRecruiter.

OA at ZipRecruiter?
Invisible during screen share
Get it