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

Reverse Linked List or Detect Cycles

MediumPython

Problem

A Kraft Heinz product catalog pipeline represents an ordered feed as a singly linked list. Given the feed values and an optional cycle position, determine whether the list contains a cycle. If it is acyclic, reverse the linked list and return its values.

Use Floyd's tortoise-and-hare algorithm for cycle detection. Do not attempt to reverse a cyclic list because traversal would not terminate.

Formal Specification

Implement reverse_if_acyclic(values, cycle_at), where values is a list of integers representing node values and cycle_at is either null or an integer index. If cycle_at is i, the final node points to the node at index i; otherwise, the final node points to null.

Return a dictionary with:

  • has_cycle: a Boolean indicating whether a cycle exists
  • values: the reversed node values if the list is acyclic, or an empty list if it is cyclic

The input list must not be modified. Build the linked list from the supplied representation before applying the algorithm.

Constraints

  • 0 <= len(values) <= 100000
  • cycle_at is null or an integer from 0 through len(values) - 1
  • -10^9 <= values[i] <= 10^9
  • The input values list must not be modified

Function Signature

def reverse_if_acyclic(values, cycle_at):
Interviewer

Your question is Reverse Linked List or 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.