Simulate a Reference-Counted Smart Pointer
Updated
Problem statement
Simulate shared-ownership smart pointers over a finite sequence of operations on named handles. All handles start empty. A managed object is destroyed exactly once when its strong reference count reaches 0.
Each operation is one space-separated string:
create h id: release the value currently owned byh, create the unique positive objectid, and makehits sole owner.copy target source: releasetarget, then make it sharesource. Copying an empty source makes the target empty.move target source: if the handles differ, releasetarget, transfer ownership fromsource, and leavesourceempty.reset h: releasehand leave it empty.get h: emitget:id, orget:-1whenhis empty.use_count h: emituse_count:count; an empty handle has count0.
Copy or move assignment from a handle to itself is a no-op. Whenever a release destroys an object, immediately emit destroy:id. After the final operation, destroy handles in reverse order of their first appearance and emit any resulting destruction events. Return all emitted strings in chronological order.
Function
simulateSharedPointer(operations: String[]) → String[]Examples
Example 1
operations = ["create a 7","copy b a","use_count a","get b","reset a","use_count b"]return = ["use_count:2","get:7","use_count:1","destroy:7"]After the copy, a and b share object 7. Resetting a leaves one owner, and final cleanup destroys the object.
Example 2
operations = ["create a 1","create b 2","move a b","get b","get a","use_count a"]return = ["destroy:1","get:-1","get:2","use_count:1","destroy:2"]Moving b into a first destroys object 1. Handle b becomes empty, and final cleanup destroys object 2.
Example 3
operations = ["create a 5","copy a a","move a a","use_count a","reset a","get a"]return = ["use_count:1","destroy:5","get:-1"]Both self-assignments are no-ops. Resetting the only owner destroys object 5, and the final query observes an empty handle.
Constraints
operationscontains at least one valid operation.- Handle names are non-empty tokens without spaces.
- Every
createuses a positive object ID that does not appear in anothercreate. - All object IDs fit in a signed 32-bit integer.
- Operations are processed in their given order.
More NVIDIA Phone Screen questions
- Network Layer Execution OrderPHONE SCREEN · Seen Nov 2026
- Paged KV Cache AccessPHONE SCREEN · Seen Nov 2026
- Sliding Window Attention over a KV CachePHONE SCREEN · Seen Nov 2026
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- LRU CacheAthenahealth · PHONE SCREEN
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Mutable Points in a Hash Set and a ListMillennium · PHONE SCREENMillennium interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Camel CardsRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions