FastPrepFewest Turns to Open a Wheel Lock
Problems/Fewest Turns to Open a Wheel Lock

Fewest Turns to Open a Wheel Lock

C3.ai logoC3.ai● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2024 report from a C3.ai new grad loop for a platform backend role says one round asked a problem similar to a well-known puzzle about opening a lock with four digit wheels. The report does not say how it differed from that puzzle.

This version is the plain puzzle. The lock starts at 0000, one turn moves one wheel by one step with 9 and 0 next to each other, a blocked code jams the lock, and you return the fewest turns or -1. If the start code or the target is blocked, the answer is -1. The limits are our choice.

Our version is about 60% the same as the reported question.

A lock has four wheels. Each wheel shows one digit from 0 to 9, so the lock always shows a four-digit code. It starts at "0000".

One turn moves one wheel one step up or down. Going up from 9 gives 0, and going down from 0 gives 9.

Some codes are listed in blocked. If the lock ever shows a blocked code, it jams and can no longer be turned. This includes the starting code: if "0000" is blocked, the lock is jammed from the start.

Return the fewest turns needed to make the lock show target without ever showing a blocked code. Return 0 if the lock already shows target and "0000" is not blocked. Return -1 if it cannot be done, including whenever "0000" or target is blocked. A blocked code can be listed more than once.

Function

fewestTurns(blocked: String[], target: String) → int

Examples

Example 1

blocked = []target = "8091"return = 4

Nothing is blocked, so each wheel can take its shortest way. The first wheel goes down from 0 to 9 to 8 (2 turns), the third wheel goes down from 0 to 9 (1 turn) and the last wheel goes up from 0 to 1 (1 turn). That is 4 turns.

Example 2

blocked = ["0001","0009"]target = "0002"return = 4

The last wheel must go from 0 to 2, but both of its neighbors, 0001 and 0009, are blocked while the other wheels show 0. So first turn another wheel: 0000 to 1000 to 1001 to 1002 to 0002. That is 4 turns, and no path with fewer turns avoids the blocked codes.

Example 3

blocked = ["0000","4321"]target = "1234"return = -1

The starting code 0000 is blocked, so the lock is jammed before the first turn. The answer is -1.

Constraints

  • 0 <= blocked.length <= 1000
  • blocked[i].length == 4 and target.length == 4
  • Every code in blocked and target is made of the digits 0 to 9 only.

More C3.ai interview questions

  • Days Until a Warmer DayPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
  • Longest Run of Ones After Flipping k ZerosPHONE SCREEN · Seen Mar 2022
  • Top K Frequent ElementsPHONE SCREEN · Seen Feb 2021
  • Item Prices From Material CostsPHONE SCREEN · Seen Dec 2020
  • Adjacent-Key Lock PatternsPHONE SCREEN · Seen Feb 2014
  • Drop Recent Duplicate MessagesOA · Seen Oct 2026
  • Maximum K-Star SumOA · Seen Oct 2026
  • Every Result of Grouping an ExpressionONSITE INTERVIEW · Seen Jan 2023
  • Fewest Jumps to the Last IndexONSITE INTERVIEW · Seen Jan 2023
  • Rotate a Square Grid ClockwiseONSITE INTERVIEW · Seen Jan 2023

Similar questions at other companies

  • Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
  • Find Duplicate Files in a FilesystemHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
  • Shortest Word Transformation SequenceReddit · PHONE SCREENReddit interview questions
  • Word LadderGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
  • Word Ladder IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Find Minimum City HopsAmazon · OA
  • Shortest Path from Encoded Graph EdgesBitGo · OABitGo interview questions
  • Open the LockMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Minimum Genetic MutationBenchling · OABenchling interview questions

Interview reports that mention this question

  • C3.ai New Grad Interviews: 25 Experiences (Part 1 of 2)

C3.ai interview experiences

  • C3.ai FDE and Other New Grad Roles: 5 Experiences (Part 2 of 2)
  • C3.ai New Grad Forward Deployed Engineer Interview: 1 Experience
All C3.ai interview questionsSee C3.ai hiring insights
Saved
class Solution {
    public int fewestTurns(String[] blocked, String target) {
        // Write your code here.
    }
}
Checking account…