Insert a Reading Into a Sorted Circular Sensor Ring

Last updated: November 29, 2026

Quick Overview

A Nest thermostat stores recent temperature readings in a sorted circular singly linked list, and you are given a pointer to an arbitrary node. Insert a new reading so the ring stays sorted, and return a node of the ring.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A Nest thermostat stores recent temperature readings in a sorted circular singly linked list, and you are given a pointer to an arbitrary node. Insert a new reading so the ring stays sorted, and return a node of the ring.

A Google onsite question known for edge cases rather than code length. The interviewer looks for the three insertion cases: between two values, at the wrap point between max and min, and in a ring where every value is equal.

What the Interviewer Expects
  • Handle an empty ring by creating a self-loop
  • Insert between prev and cur when prev <= x <= cur
  • Insert at the max-to-min wrap if x is a new max or new min
  • Stop after one full lap for all-equal rings
  • O(n) time, O(1) space
Key Topics to Cover
circular linked list
sorted insertion
edge cases
invariants
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Support deletion of a value while keeping the ring sorted.
  • Readings arrive at high rate; how would you avoid O(n) inserts?
  • Make the ring bounded and evict the oldest reading.
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: node (any node of the ring, or None) and value x.
  • Output: a node in the ring after inserting x in sorted position.
  • O(n) time, O(1) extra space....
High-Level Design

Walk pairs (prev, cur) around the ring starting at the given node. Three cases allow insertion between prev and cur: (1) prev.val <= x <= cur.val, the normal case; (2) prev.val > cur.val, meaning ...


Submit Your Answer
Markdown supported