BackeasyLinked ListCapgeminiMeesho

Monotonic Target Index Solution

Problem Statement

You are provided with a singly linked list of integers that is guaranteed to contain a cycle. The cycle is formed because the next pointer of the last node in the list points back to a previous node within the sequence. Your objective is to identify the 0-based index of the node where the cycle begins. This specific index is referred to as the Monotonic Target Index.

The input is represented as an array of integers where the last element indicates the index of the node that the tail points to. If the last element is -1, there is no cycle, but for this problem, a cycle is always present. The index of the cycle start is the position of the first node encountered when traversing from the head that is part of the circular loop.

Return the integer index of the cycle's starting node. If the list were acyclic (which is not the case here), the return value would be -1, but you may assume the cycle exists.

Example 1
Input
nums = [3, 8, 2, 1, 0]
Output
0

Explanation: The list is 3 -> 8 -> 2 -> 1 -> 3. The tail node (value 1) points to the head (value 3). The cycle starts at index 0. Therefore, the Monotonic Target Index is 0.

Example 2
Input
nums = [5, 6, 7, 8, 1]
Output
1

Explanation: The list is 5 -> 6 -> 7 -> 8 -> 6. The tail node (value 8) points to the node at index 1 (value 6). The cycle starts at index 1. Therefore, the Monotonic Target Index is 1.

Example 3
Input
nums = [10, 20, 30, 40, 2]
Output
2

Explanation: The list is 10 -> 20 -> 30 -> 40 -> 30. The tail node (value 40) points to the node at index 2 (value 30). The cycle starts at index 2. Therefore, the Monotonic Target Index is 2.

Example 4
Input
nums = [1, 2, 3, 4, 3]
Output
3

Explanation: The list is 1 -> 2 -> 3 -> 4 -> 4. Wait, the last element is 3, meaning the tail points to index 3. The list is 1 -> 2 -> 3 -> 4 -> 4. The cycle starts at index 3. Therefore, the Monotonic Target Index is 3.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • The last element of nums is the index of the node the tail points to.
  • The last element of nums is always between 0 and nums.length - 1, ensuring a cycle exists.
  • All node values are unique except for the cycle structure implied by the pointer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Monotonic Target Index — Problem Statement & Solution Guide

Linked ListEasyFloyd Cycle Detection
TimeO(N)
|
SpaceO(1)

Problem Description

You are provided with a singly linked list of integers that is guaranteed to contain a cycle. The cycle is formed because the next pointer of the last node in the list points back to a previous node within the sequence. Your objective is to identify the 0-based index of the node where the cycle begins. This specific index is referred to as the Monotonic Target Index.

The input is represented as an array of integers where the last element indicates the index of the node that the tail points to. If the last element is -1, there is no cycle, but for this problem, a cycle is always present. The index of the cycle start is the position of the first node encountered when traversing from the head that is part of the circular loop.

Return the integer index of the cycle's starting node. If the list were acyclic (which is not the case here), the return value would be -1, but you may assume the cycle exists.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Monotonic Target Index"

easy

WHY DOES IT MATTER?

Detecting cycles and their entry points is a fundamental pattern for reasoning about linked data structures, graph traversals, and even memory‑leak detection. Mastery of this pattern demonstrates an ability to reason about pointer dynamics and invariant preservation under limited resources.

OPTIMIZATION CHALLENGE

The key insight is that the relative speed difference between two pointers creates a deterministic offset that aligns with the loop length, allowing the algorithm to locate the entry without extra storage—turning a potentially quadratic or linear‑space solution into linear‑time, constant‑space.

REAL-WORLD CONNECTION

In distributed systems, heartbeat messages form a logical ring; detecting when a node re‑joins the same ring (a cycle) and pinpointing the re‑entry point mirrors the linked‑list cycle start problem, helping to avoid duplicate processing or infinite loops.

During an interview, first articulate the detection phase, then explicitly state why resetting one pointer works; drawing a quick diagram on the whiteboard can make the invariant clear and earn you extra points for communication.

COMPLEXITY AT A GLANCE

⏱ Time:O(N)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem of locating the entry point of a cycle in a singly linked list is a classic application of Floyd’s Tortoise and Hare algorithm, also known as the two‑pointer technique. The algorithm first detects a cycle by moving two pointers at different speeds (slow advances one step, fast advances two steps) until they meet; this meeting point guarantees that a cycle exists. Once a collision is found, resetting one pointer to the head and advancing both pointers one step at a time will cause them to converge exactly at the node where the cycle begins, because the distance from the head to the cycle start equals the distance from the collision point to the cycle start along the loop. Naïve solutions—such as storing visited node references in a hash set or repeatedly traversing the list to count lengths—require O(N) extra space or O(N²) time, which become prohibitive for large inputs. Floyd’s method achieves O(N) time with O(1) auxiliary space, making it optimal for this monotonic target index problem.

Interview Questions on This Problem

Q1How does Floyd’s cycle detection algorithm guarantee that the second phase finds the exact start of the loop?

After the first meeting, the distance the slow pointer has traveled equals μ + k·λ (μ = distance to loop start, λ = loop length, k ≥ 1). The fast pointer has traveled 2·(μ + k·λ). Their difference is a multiple of λ, so when we reset one pointer to the head and move both at speed 1, they each travel μ steps before meeting at the loop entry.

Q2Can you modify the algorithm to return the length of the cycle as well as the entry index?

Yes. After detecting the meeting point, keep the fast pointer stationary and move the slow pointer around the loop until it returns to the meeting node, counting steps; this count is λ, the cycle length. Then use the standard two‑pointer reset to locate the entry index.

Q3Why is using a hash set to record visited nodes considered sub‑optimal for this problem in a production system?

A hash set incurs O(N) additional memory, which can be significant for large linked structures and may cause cache pressure. Moreover, it adds overhead for hashing and look‑ups, whereas the constant‑space Floyd approach is deterministic, faster, and avoids extra allocations, which is critical in low‑latency services.

Examples

Example 1

Input

nums = [3, 8, 2, 1, 0]

Output

0

Explanation: The list is 3 -> 8 -> 2 -> 1 -> 3. The tail node (value 1) points to the head (value 3). The cycle starts at index 0. Therefore, the Monotonic Target Index is 0.

Example 2

Input

nums = [5, 6, 7, 8, 1]

Output

1

Explanation: The list is 5 -> 6 -> 7 -> 8 -> 6. The tail node (value 8) points to the node at index 1 (value 6). The cycle starts at index 1. Therefore, the Monotonic Target Index is 1.

Example 3

Input

nums = [10, 20, 30, 40, 2]

Output

2

Explanation: The list is 10 -> 20 -> 30 -> 40 -> 30. The tail node (value 40) points to the node at index 2 (value 30). The cycle starts at index 2. Therefore, the Monotonic Target Index is 2.

Example 4

Input

nums = [1, 2, 3, 4, 3]

Output

3

Explanation: The list is 1 -> 2 -> 3 -> 4 -> 4. Wait, the last element is 3, meaning the tail points to index 3. The list is 1 -> 2 -> 3 -> 4 -> 4. The cycle starts at index 3. Therefore, the Monotonic Target Index is 3.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^9
  • The last element of nums is the index of the node the tail points to.
  • The last element of nums is always between 0 and nums.length - 1, ensuring a cycle exists.
  • All node values are unique except for the cycle structure implied by the pointer.

Optimal Approach & Strategy

Use Floyd’s two‑pointer technique: first detect a collision, then reset one pointer to the head and move both one step at a time to converge on the entry node, achieving O(1) space.

Brute Force Approach

Store every visited node in a hash set and stop when you encounter a node already present; its index is the cycle start. This uses O(N) extra memory.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function monotonicTargetIndex(nums) {
   let monotonicTargetIndex = 0;
   for (let num of nums) {
       monotonicTargetIndex += num;
   }
   return monotonicTargetIndex;
}

Asked in Top Tech Interviews

CapgeminiMeesho

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.