Problem · Array

Tool Changer

Learn this problem
EasySalesforce logoSalesforceOA
See Salesforce hiring insights

Problem statement

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

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

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

Examples

Example 1

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

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

🐕‍🦺🐕

More Salesforce problems

drafts saved locally
public int toolchanger(String[] tools, int startIndex, String target) {
  // write your code here
}
tools["ballendmill", "keywaycutter", "slotdrill", "facemill"]
startIndex1
target"ballendmill"
expected1
checking account