FastPrepMinimum Steps on a Circular Character Dial

Minimum Steps on a Circular Character Dial

Navan logoNavan● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

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 = 2target = "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 = 1target = "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 = 2target = "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.

More Navan problems

See Navan hiring insights
public int calculateMinSteps(String ring, int startIndex, String target) {
    // write your code here
}
ring"ABCDEF"
startIndex2
target"FA"
expected4
Checking account…