Reported January 2024
Navandynamic programming

Minimum Steps on a Circular Character Dial

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

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

The mistake that sinks a first attempt on this Navan OA, reported in January 2024, is going greedy: grabbing the nearest occurrence of each target character and moving on. It looks right on Example 1 and falls apart the moment the ring has duplicates, like "ABACA". You've got a circular dial, a start index, and a target to hit in order. It's a string problem wearing a DP costume. If you blank on the state definition during the live assessment, StealthCoder is the safety net that stays invisible on screen while you recover. But the pattern is small enough to own tonight.

The problem

You are given a circular array of characters as ring, a starting index startIndex, and a target string. Moving clockwise or counterclockwise to an adjacent ring position costs one step.
For this exercise, assume you must visit the target characters in order. Visiting a character costs no extra selection step. The ring may contain duplicate characters, and you may choose any occurrence when visiting the next target character. Return the minimum total movement cost, or -1 if a target character is absent. An empty target costs zero steps.

Function
calculateMinSteps(ring: String, startIndex: int, target: String) → int

Examples
Example 1
ring = "ABCDEF"
startIndex = 2
target = "FA"
return = 4
From C at index 2, reach F in three moves in either direction, then A in one move. The total is four.
Example 2
ring = "ABACA"
startIndex = 1
target = "AC"
return = 2
Choose A at index 2, one step from B, then C at index 3 in one more step. Choosing A at index 0 would cost more before the following C.
Example 3
ring = "XYZ"
startIndex = 2
target = "A"
return = -1
A does not occur on the supplied ring.

Constraints
1 <= ring.length <= 100.
0 <= startIndex < ring.length.
0 <= target.length <= 100.
For this exercise, assume ring and target characters are ASCII letters and digits.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is dynamic programming over target positions. Let dp[i][j] be the minimum cost to have visited target[0..i] while standing on ring index j, where ring[j] equals target[i]. For each j that matches target[i], take the minimum over every k matching target[i-1] of dp[i-1][k] plus the circular distance between k and j. Circular distance is min(|a-b|, n-|a-b|). Seed the first layer from startIndex. The pitfall is greedy nearest-occurrence, which fails when a farther A sits closer to the next C, exactly Example 2. Also handle the edge cases: empty target returns 0, and any target character missing from the ring returns -1. With ring and target both capped at 100, the O(m * n * n) cost is trivial. If the recurrence slips away mid-OA, StealthCoder can feed you the full solution in real time.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Minimum Steps on a Circular Character Dial 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Navan reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Steps on a Circular Character Dial FAQ

What's the trick in the Navan circular dial problem?+

Don't go greedy. Duplicate characters mean the nearest occurrence of the next target letter can lead to a worse path later. Use DP where state is the target index plus which ring position you're standing on, and take the minimum over all previous matching positions.

How hard is this really?+

Medium. The code is short, but you have to spot that greedy fails. Once you see the DP state, it's about 20 lines. The constraints, 100 for both ring and target, let a cubic solution pass easily.

How do I compute distance on a circular ring?+

Take d = abs(a - b), then the answer is min(d, n - d), where n is the ring length. That covers clockwise and counterclockwise in one expression. Forgetting the wraparound is the second most common bug after going greedy.

What edge cases should I test before submitting?+

Empty target returns 0. A target character not on the ring returns -1, like Example 3. Start index already sitting on the first target letter costs zero. Also test a ring with repeated letters, like Example 2, to confirm you're not picking the nearest occurrence blindly.

How do I prepare in 48 hours for a problem like this?+

Write this one from scratch twice, without peeking. Focus on defining the DP state out loud before coding. Then do two or three other layered-choice DP problems where each step picks among duplicates. The pattern repeats more than the specific problem does.

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

OA at Navan?
Invisible during screen share
Get it