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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Create Linked List and Detect Cycles
00:00
5 left

Create Linked List and Detect Cycles

MediumPython

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.