FastPrepFastPrep
Problem Brief

Tool Changer

OA
See Salesforce online assessment and hiring insights

A milling machine in a manufacturing facility has a tool change system. The tool changer holds n tools and some duplicate tools may be included. The operator must move through the tools one at a time, either moving left or right. The tool changer is circular, so when the last tool in the tools array is reached in either direction, the next tool is at the other end of the array.

Given the name of the next tool needed, determine the minimum number of left or right moves to reach it.

Function Description

Complete the function toolchanger in the editor.

toolchanger has the following parameter(s):

  1. String[] tools: an array of tool names arranged in the order they appear in the tool changer
  2. int startIndex: index of the tool currently in use
  3. String target: name of the tool needed

Returns

int: the minimum number of moves

1Example 1

Input
tools = ["ballendmill", "keywaycutter", "slotdrill", "facemill"], startIndex = 1, target = "ballendmill"
Output
1
Explanation

The tool currently in use is keywaycutter at index 1. The desired tool is ballendmill at index 0. It can be reached by moving right 3 steps or left 1 step. The minimum number of moves is 1.

Constraints

Limits and guarantees your solution can rely on.

๐Ÿ•โ€๐Ÿฆบ๐Ÿ•
public int toolchanger(String[] tools, int startIndex, String target) {
  // write your code here
}
Input

tools

["ballendmill", "keywaycutter", "slotdrill", "facemill"]

startIndex

1

target

"ballendmill"

Output

1

Sign in to submit your solution.