Maintain a Consistent-Hashing Ring
Updated
Problem statement
Keep track of the nodes in a consistent-hashing ring and which node owns each key. Positions increase clockwise, and the last position wraps back to the first. The input gives the integer hash positions. You do not need to write a hash function.
The ring starts empty. Process the matching arrays operations and positions from left to right. At index i, apply exactly one operation:
ADD: add a node atpositions[i]. A node is identified by its position and has exactly one ring point. Adding an existing position has no effect.REMOVE: remove the node atpositions[i]. Removing a missing node has no effect.LOOKUP: interpretpositions[i]as a key position. Its owner is the active node with the smallest position greater than or equal to that key. If there is no such node, wrap to the active node with the smallest position. If the ring is empty, return-1for this lookup.
Return an integer array containing only the LOOKUP results in the order of the lookups. Do not change either input array. Each operation uses the nodes present after the earlier operations. A node added later is not yet available.
Function
runConsistentHashRing(operations: String[], positions: int[]) → int[]Examples
Example 1
operations = ["ADD","ADD","LOOKUP","LOOKUP","LOOKUP"]positions = [10,30,10,11,35]return = [10,30,10]Position 10 owns its exact key. The next clockwise node after 11 is 30, and 35 wraps to 10.
Example 2
operations = ["LOOKUP","ADD","ADD","REMOVE","LOOKUP","REMOVE","ADD","LOOKUP"]positions = [5,20,20,20,0,20,0,1]return = [-1,-1,0]The first lookup finds an empty ring. Adding node 20 twice still creates one member, so one removal empties the ring. Removing that missing member does nothing. The final lookup wraps to node 0.
Example 3
operations = ["ADD","ADD","LOOKUP","ADD","LOOKUP","REMOVE","LOOKUP"]positions = [10,30,20,25,20,25,20]return = [30,25,30]The key at 20 is first assigned to 30, then to newly added node 25, and back to 30 after node 25 is removed.
Constraints
0 <= operations.length <= 100000.positions.length == operations.length.- Every operation is
ADD,REMOVE, orLOOKUP. 0 <= positions[i] <= 1000000000.- Positions and the returned node identifiers are integers.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Maintain a Consistent-Hashing RingCohesity · ONSITE INTERVIEW
- Top K Pins with Insert and QueryPinterest · PHONE SCREENPinterest interview questions
- Time Map with Sum and AverageConfluent · PHONE SCREENConfluent interview questions
- Structured Log Stream with QueriesOpenAI · PHONE SCREENOpenAI interview questions
- Interview Slot BookingTrueFoundry · PHONE SCREEN
- Next Upcoming BirthdayAdobe · PHONE SCREENAdobe interview questions
- Average Prices from Broker QuotesAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Out-of-Order Logger Rate LimiterGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)