FastPrepTool Changer

Tool Changer

IBM logoIBM● EasyNEW GRADINTERNOA
Learn

Problem statement

A milling machine has a circular tool changer containing n tools. A tool name may appear more than once.

The operator can move through the changer one position at a time, either left or right. The changer is circular, so moving past one end wraps around to the other end.

You are given:

  • tools, the tool names in circular order;
  • startIndex, the index of the tool currently in use; and
  • target, the required tool name.

Return the minimum number of one-position moves needed to reach any occurrence of target from startIndex.

Function

toolchanger(tools: String[], startIndex: int, target: String) → int

Examples

Example 1

tools = ["ballendmill", "keywaycutter", "slotdrill", "facemill"]startIndex = 1target = "ballendmill"return = 1

The current tool is keywaycutter at index 1. The target ballendmill is at index 0. Moving left takes 1 step, while moving right takes 3 steps, so the answer is 1.

Example 2

tools = ["ballendmill", "facemill", "keywaycutter", "slotdrill"]startIndex = 1target = "slotdrill"return = 2

The current tool is facemill at index 1. The target slotdrill is at index 3. Moving left or right takes 2 steps, so the answer is 2.

Constraints

  • 1 <= tools.length <= 100
  • 0 <= startIndex < tools.length
  • 1 <= tools[i].length, target.length <= 100
  • target appears at least once in tools.

More IBM problems

See IBM hiring insights
public int toolchanger(String[] tools, int startIndex, String target) {
  // Write your code here
}
tools["ballendmill", "keywaycutter", "slotdrill", "facemill"]
startIndex1
target"ballendmill"
expected1
Checking account…