Tool Changer
Updated
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):
String[] tools: an array of tool names arranged in the order they appear in the tool changerint startIndex: index of the tool currently in useString 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 = 1The 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 interview questions
- Count Subarrays With Product Equal to KOA · Seen Dec 2026
- Get Minimum TimeOA · Seen Nov 2026
- Count Increasing TripletsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Reverse a Sublist of a Linked ListONSITE INTERVIEW · Seen Nov 2026
- Rotting OrangesONSITE INTERVIEW · Seen Nov 2026
- 3Sum SmallerONSITE INTERVIEW · Seen Nov 2026
- Count Zero Request ServersONSITE INTERVIEW · Seen Nov 2026
- Minimum No-Repeat Segments After One Character RemovalOA · Seen Nov 2026
- Get Message StatusOA · Seen Nov 2026
Similar questions at other companies
- Theme Park Shuttle Wait TimeAirbnb · OAAirbnb interview questions
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Lift Total TravelInfosys · OAInfosys interview questions
- Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions