Insert into a Sorted Circular List
Updated
Problem statement
A nondecreasing circular singly linked list is serialized by one finite traversal cycle, beginning at an arbitrary node. Therefore the values have at most one descending wrap edge.
Insert insertValue while preserving the circular nondecreasing order. Duplicates are allowed. Return one traversal of the resulting cycle:
- For a nonempty input, begin at the same original first node.
- For an empty input, return the one-node cycle containing
insertValue.
When more than one edge can accept the value, insert after the first such edge encountered from the serialized first node. The returned array lists the circular list once around the cycle.
Function
insertIntoSortedCircular(cycle: int[], insertValue: int) → int[]Examples
Example 1
cycle = [3,4,1]insertValue = 2return = [3,4,1,2]The traversal wraps from 4 to 1. Inserting 2 between 1 and the original first node 3 preserves circular order.
Example 2
cycle = [1,3,4]insertValue = 2return = [1,2,3,4]The value belongs on the ordinary ascending edge from 1 to 3.
Example 3
cycle = []insertValue = 5return = [5]An empty list becomes a one-node cycle.
Constraints
0 <= cycle.length <= 200000.-10^9 <= cycle[i], insertValue <= 10^9.- The nonempty input has at most one index
ifor whichcycle[i] > cycle[(i + 1) % cycle.length].
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Deep Copy a Random-Pointer ListAmazon · ONSITE INTERVIEW
- Merge One List Between Two PositionsOracle · ONSITE INTERVIEWOracle interview questions
- Insert into an Unrolled Linked ListIndeed · PHONE SCREEN · ONSITE INTERVIEWIndeed interview questions
- Deep Copy a Random-Pointer ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Pack Stream Chunks into Fixed-Capacity NodesBroadcom Limited · ONSITE INTERVIEW
- Minimize Task Resource ConsumptionNVIDIA · OANVIDIA interview questions
- Largest Microorganism After ConsumptionOpenAI · OAOpenAI interview questions
- Buffet Day GainsUpstart · OAUpstart interview questions
- Deep Copy a Random-Pointer ListSuperhuman · PHONE SCREENSuperhuman interview questions
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW