Find the Minimum and Maximum Number of Nodes Between Critical Points

MediumLinked Listsimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

In a linked list, a **critical point** is a node that has both a previous and a next node and is either a **local maximum** (strictly greater than both neighbors) or a **local minimum** (strictly smaller than both neighbors). The first and last nodes are never critical. Given the head of a linked list, return a two-element array [minDistance, maxDistance], where minDistance is the smallest distance between any two distinct critical points and maxDistance is the largest. Distance is the difference of the nodes' positions in the list. If the list has fewer than two critical points, return [-1, -1].

Examples

Input: head = [3,1]
Output: [-1,-1]
With only two nodes, neither has two neighbors.
Input: head = [2,5,1,4,0,6]
Output: [1,3]
Positions 1 through 4 alternate between local maxima and minima. The closest pair is 1 apart; the farthest (positions 1 and 4) is 3 apart.
Input: head = [1,3,2,2,3,2,2,2,7]
Output: [3,3]
Only positions 1 and 4 are critical. Nodes equal to a neighbor are not strict extrema.

Constraints

  • 2 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems