Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Minimum Turns Lock Problem
00:00
5 left

Minimum Turns Lock Problem

MediumPython

Problem

The Current app uses a four-wheel security lock for a protected flow. The lock starts at "0000". Each turn increments or decrements exactly one wheel, with digits wrapping from 9 to 0 and from 0 to 9.

Given a list of forbidden lock combinations and a target combination, return the minimum number of turns needed to reach the target from "0000". Return -1 if the target cannot be reached. A forbidden combination cannot be entered, including the starting combination.

Formal Specification

Implement open_lock(deadends, target).

  • deadends is a list of unique four-character strings containing digits 0 through 9.
  • target is a four-character string containing digits 0 through 9.
  • Return an integer representing the minimum number of turns, or -1 when no valid sequence exists.
  • A move changes one digit by exactly +1 or -1, with wraparound.

Constraints

  • 0 <= len(deadends) <= 10,000
  • Every deadend is a unique four-character string of digits.
  • target is a four-character string of digits.
  • The state space contains exactly 10,000 possible combinations.

Function Signature

def open_lock(deadends, target):
Interviewer

Your question is Minimum Turns Lock Problem. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.