Process Singly Linked List Operations
Updated
Problem statement
Implement a singly linked list and process two equal-length arrays of operations. For operation type 1, insert values[i] at the front. For type 2, insert it at the back. For type 3, remove the front node when the list is nonempty; its paired value is ignored. Return the list values from front to back after all operations.
Maintain both head and tail pointers so each operation runs in constant time.
Function
processSinglyLinkedListOperations(operationTypes: int[], values: int[]) → int[]Examples
Example 1
operationTypes = [2,2,1,3]values = [4,7,2,0]return = [4,7]Append four and seven, prepend two, then remove the front node.
Example 2
operationTypes = [3,2,3,3]values = [0,8,0,0]return = []Removing from an empty list is a no-op; the later inserted node is removed once.
Constraints
1 <= operationTypes.length == values.length <= 200000operationTypes[i] is 1, 2, or 3values[i] is a signed 32-bit integervalues[i] is ignored when operationTypes[i] is 3
More SEDEMAC interview questions
Similar questions at other companies
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Head-Inserted Linked-List Hash ChainGoogle · ONSITE INTERVIEWGoogle interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Recover a Function-Call LRU Cache with a Write-Ahead LogAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions