Copy a Doubly Linked List with a Special Pointer
Updated
Problem statement
A doubly linked list has one node per row of nodes. Row i is [value, specialIndex]:
- The node's
leftpointer targets rowi - 1, or is null for the first row. - The node's
rightpointer targets rowi + 1, or is null for the final row. specialIndexis the row targeted by the node's arbitraryspecialpointer, or-1for null.
Build the list and create a deep copy. Every copied left, right, and special pointer must target only copied nodes.
After copying, change the original node at mutationIndex: set its value to newValue and redirect its special pointer to newSpecialIndex, where -1 means null. Return two encoded snapshots in this order: the original list after the change, then the unchanged deep copy.
Function
copyDoublyListWithSpecialPointer(nodes: int[][], mutationIndex: int, newValue: int, newSpecialIndex: int) → int[][][]Examples
Example 1
nodes = [[7,2],[13,-1],[11,0]]mutationIndex = 1newValue = 99newSpecialIndex = 2return = [[[7,2],[99,2],[11,0]],[[7,2],[13,-1],[11,0]]]The middle original node changes to [99,2]. The copied middle node remains [13,-1], proving that the copy is independent.
Example 2
nodes = [[5,0]]mutationIndex = 0newValue = 8newSpecialIndex = -1return = [[[8,-1]],[[5,0]]]The original loses its self-reference, while the copied node keeps its value and a special pointer to itself.
Example 3
nodes = [[1,3],[2,0],[3,1],[4,2]]mutationIndex = 2newValue = 30newSpecialIndex = 3return = [[[1,3],[2,0],[30,3],[4,2]],[[1,3],[2,0],[3,1],[4,2]]]The special pointers form a cycle. The mutation changes only the original third node; the cloned cycle stays unchanged.
Constraints
1 <= nodes.length <= 50000.- Every row contains exactly
[value, specialIndex]. - All node values and
newValuefit a signed 32-bit integer. - Every stored special index and
newSpecialIndexis-1or a valid row index. 0 <= mutationIndex < nodes.length.- The returned deep-copy snapshot must retain every pre-mutation value and special link.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Count Linked-List IslandsGoogle · ONSITE INTERVIEWGoogle interview questions
- Merge Event PreferencesAirbnb · PHONE SCREEN · ONSITE INTERVIEWAirbnb interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Package Install OrderVerily · ONSITE INTERVIEW
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)