BackhardLinked ListAtlassianCred

Monotonic Stream Minimum Solution

Problem Statement

You are given a singly linked list of integers. The list may contain a cycle, i.e., the tail node may point back to an earlier node. Your task is to determine the smallest integer value that appears in any node reachable from the head of the list. The algorithm must terminate even when a cycle is present, and it should use only constant additional memory.

Input: The list is described by two pieces of information: an array of integer values representing the nodes in the order they are linked, and an integer cycle that is the zero‑based index of the node to which the tail connects. If cycle is -1, the list is acyclic. The head of the list is the first element of the array.

Output: A single integer, the minimum value among all nodes reachable from the head.

The solution should run in linear time relative to the number of distinct nodes and use O(1) auxiliary space. A standard approach is to first use Floyd’s cycle‑finding algorithm to detect a cycle and, if one exists, to locate the entry point of the cycle. Once the cycle is identified, a single traversal that visits each node exactly once (including the cycle nodes) yields the minimum value.

The problem is considered hard because it combines cycle detection with a global property (minimum) that must be computed without revisiting nodes, and because the input representation requires careful handling of the cycle index.

Example 1
Input
{"nodes":[3,1,4,2],"cycle":-1}
Output
1

Explanation: The list is 3 → 1 → 4 → 2 → null. There is no cycle. A single pass visits all four nodes and finds the smallest value, 1.

Example 2
Input
{"nodes":[5,3,7,2,9],"cycle":1}
Output
2

Explanation: The list is 5 → 3 → 7 → 2 → 9 → (back to node with value 3). Floyd’s algorithm detects the cycle. Traversing the nodes once (5,3,7,2,9) yields the minimum value 2.

Example 3
Input
{"nodes":[-5,-10,-3,-10],"cycle":1}
Output
-10

Explanation: The list is -5 → -10 → -3 → -10 → (back to node with value -10). The cycle includes the two nodes with value -10. Visiting each distinct node gives the minimum -10.

Example 4
Input
{"nodes":[42],"cycle":-1}
Output
42

Explanation: A single-node list with no cycle. The only value is 42, which is the minimum.

Constraints

  • 1 <= nodes.length <= 100000
  • -1000000000 <= nodes[i] <= 1000000000
  • cycle == -1 or 0 <= cycle < nodes.length
  • The list contains at most one cycle.
  • All node values are integers.
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 Stream Minimum — Problem Statement & Solution Guide

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

Problem Description

You are given a singly linked list of integers. The list may contain a cycle, i.e., the tail node may point back to an earlier node. Your task is to determine the smallest integer value that appears in any node reachable from the head of the list. The algorithm must terminate even when a cycle is present, and it should use only constant additional memory.

Input: The list is described by two pieces of information: an array of integer values representing the nodes in the order they are linked, and an integer cycle that is the zero‑based index of the node to which the tail connects. If cycle is -1, the list is acyclic. The head of the list is the first element of the array.

Output: A single integer, the minimum value among all nodes reachable from the head.

The solution should run in linear time relative to the number of distinct nodes and use O(1) auxiliary space. A standard approach is to first use Floyd’s cycle‑finding algorithm to detect a cycle and, if one exists, to locate the entry point of the cycle. Once the cycle is identified, a single traversal that visits each node exactly once (including the cycle nodes) yields the minimum value.

The problem is considered hard because it combines cycle detection with a global property (minimum) that must be computed without revisiting nodes, and because the input representation requires careful handling of the cycle index.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Monotonic Stream Minimum"

hard

WHY DOES IT MATTER?

Mastering cycle detection paired with global property tracking demonstrates deep proficiency in multi-pointer memory management under strict space constraints.

OPTIMIZATION CHALLENGE

Inspecting every reachable node exactly once without storing visited addresses in a HashSet or altering list pointers.

REAL-WORLD CONNECTION

Essential in embedded software development, high-frequency low-latency systems, and garbage collectors where cyclic memory graphs must be swept without allocating heap space.

Separate the problem into clear stages: first detect cycle presence; if present, identify the entry node; then perform a deterministic single-pass reduction across the prefix and loop.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

Traversal of a potentially cyclic linked list without modifying node pointers or allocating auxiliary hash sets requires solving two simultaneous challenges: detecting cycle boundaries and guaranteeing full node coverage. A naive linear traversal without cycle detection will loop indefinitely on cyclic structures, while a standard hash set approach consumes O(N) auxiliary space to track visited nodes, violating constant memory constraints.

The optimal strategy leverages Floyd's Cycle-Finding Algorithm (Tortoise and Hare) combined with bounded state aggregation. Floyd's algorithm utilizes a slow pointer moving one step at a time and a fast pointer moving two steps at a time. If the list is acyclic, the fast pointer reaches null in O(N) time. If a cycle exists, the fast and slow pointers are guaranteed to meet within the cycle loop in O(N) time, operating entirely in O(1) auxiliary space.

Because the fast pointer skips every second node during cycle detection, tracking node values strictly during phase 1 is insufficient. Once an intersection is found, phase 2 resets one pointer to the list head to identify the exact cycle entry node. Traversing from head to cycle entry, followed by a single complete traversal around the cycle loop, ensures every unique reachable node is evaluated exactly once or twice, yielding a strictly optimal O(N) time and O(1) space solution.

Interview Questions on This Problem

Q1How would you find the minimum node value if modifying the node structure (e.g., adding a visited flag) was permitted?

If structural mutation is allowed, we could set a visited flag or mutate node values to a sentinel value during standard traversal, tracking the minimum until a visited node is encountered. However, Floyd's cycle detection is strictly superior because it leaves the original data structure intact while maintaining O(N) time and O(1) auxiliary space.

Q2Why is tracking the minimum value using only the slow pointer during Floyd's phase 1 insufficient?

During phase 1, fast and slow pointers meet inside the cycle, but the slow pointer may not have traversed every node in the cycle or even reached the cycle entry before the meeting point. To guarantee every node value is evaluated, we must explicitly iterate through the linear prefix to the cycle start, followed by one full loop through the cycle.

Q3How does Brent's Cycle Detection algorithm compare to Floyd's for this specific problem?

Brent's algorithm uses a moving power-of-two window to detect cycles using up to 36% fewer pointer steps on average compared to Floyd's algorithm. However, both achieve O(N) time and O(1) space complexity, and Floyd's algorithm is typically preferred in interviews due to its intuitive pointer arithmetic and lower mental overhead.

Examples

Example 1

Input

{"nodes":[3,1,4,2],"cycle":-1}

Output

1

Explanation: The list is 3 → 1 → 4 → 2 → null. There is no cycle. A single pass visits all four nodes and finds the smallest value, 1.

Example 2

Input

{"nodes":[5,3,7,2,9],"cycle":1}

Output

2

Explanation: The list is 5 → 3 → 7 → 2 → 9 → (back to node with value 3). Floyd’s algorithm detects the cycle. Traversing the nodes once (5,3,7,2,9) yields the minimum value 2.

Example 3

Input

{"nodes":[-5,-10,-3,-10],"cycle":1}

Output

-10

Explanation: The list is -5 → -10 → -3 → -10 → (back to node with value -10). The cycle includes the two nodes with value -10. Visiting each distinct node gives the minimum -10.

Example 4

Input

{"nodes":[42],"cycle":-1}

Output

42

Explanation: A single-node list with no cycle. The only value is 42, which is the minimum.

Constraints

  • 1 <= nodes.length <= 100000
  • -1000000000 <= nodes[i] <= 1000000000
  • cycle == -1 or 0 <= cycle < nodes.length
  • The list contains at most one cycle.
  • All node values are integers.

Optimal Approach & Strategy

Execute Floyd's Tortoise and Hare algorithm to detect cycle presence while updating the minimum value. If a cycle exists, locate the cycle entrance node and execute a single deterministic loop pass to evaluate all unique reachable values in O(N) time and O(1) space.

Brute Force Approach

Store node references in a Hash Set while traversing the list and updating the running minimum. Terminate as soon as a duplicate node reference is detected in the set.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(nums) { return Math.min(...nums); }

Asked in Top Tech Interviews

AtlassianCred

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.