Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Coding: Linked List Loop Detection

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Coding: Linked List Loop Detection. Start with the requirements on the right.

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.

Problem

A Check Point security event pipeline stores events in a singly linked list. Implement the list construction and determine whether the list contains a loop, such as one caused by an incorrect event-linking operation.

The function receives a list of event identifiers and a loop position. It must build the linked list, connect the final node to the node at that position when applicable, and detect the loop using constant auxiliary space.

Formal Specification

Implement has_loop(values, loop_index). values is a list of integers used to create one node per value. loop_index is the zero-based index of the node that the final node should reference, or -1 when the list should terminate at None. Return True if the constructed list contains a loop, otherwise return False.

Use a singly linked-list node with value and next fields. Do not traverse indefinitely, and do not use a set of visited nodes in the primary solution.

Constraints

  • 0 <= len(values) <= 10^5
  • -10^9 <= values[i] <= 10^9
  • loop_index is -1 or a valid index in values
  • The input describes at most one loop

Function Signature

def has_loop(values, loop_index):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output