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
Cycle Detection in Doubly Linked List
00:00
5 left

Cycle Detection in Doubly Linked List

MediumPython

Problem

CareerBuilder's candidate profile navigation uses doubly linked structures. Given a serialized doubly linked list, determine whether any cycle is reachable from the head through either next or prev links. The structure may be malformed, so do not assume that prev links are exact reverses of next links.

Use Floyd's tortoise-and-hare algorithm rather than storing visited nodes, and do not modify the input.

Formal Specification

Implement has_cycle(next_links, prev_links, head), where next_links and prev_links are arrays of equal length. Each array entry is either an integer node index or None. head is an integer index or None. Return True if repeatedly following next from head eventually repeats a node, or if repeatedly following prev from head eventually repeats a node. Otherwise, return False.

Constraints

  • 0 <= len(next_links) <= 10^6
  • len(next_links) == len(prev_links)
  • head is None or a valid node index
  • Every non-None link is a valid node index
  • The input is not modified

Function Signature

def has_cycle(next_links, prev_links, head):
Interviewer

Your question is Cycle Detection in Doubly Linked List. 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.