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
Create Linked List and Detect Cycles
00:00
5 left

Create Linked List and Detect Cycles

EasyPython

Problem

The Gap app receives a sequence of product record IDs that must be represented as a singly linked list. Implement the list construction and determine whether the resulting list contains a cycle.

A cycle is created when the final node points back to an earlier node. The input identifies that earlier node by its zero-based index. Use Floyd's tortoise-and-hare algorithm for cycle detection, do not use a set or dictionary of visited nodes, and do not modify node links after construction.

Formal Specification

Implement has_cycle(values, cycle_index). values is a list of integers used to create one node per value. cycle_index is an integer: -1 means the final node points to None; otherwise, the final node must point to the node at that zero-based index. Return True if the constructed list contains a cycle, and False otherwise.

Constraints

  • 0 <= len(values) <= 100000
  • -10^9 <= values[i] <= 10^9
  • cycle_index is -1 or a valid index in values
  • The linked list must be constructed from custom nodes
  • The cycle detection phase must use O(1) auxiliary space

Function Signature

def has_cycle(values, cycle_index):
Interviewer

Your question is Create Linked List and Detect Cycles. 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.