What error will occur when enqueue is called on an empty queue?
medium
A. ReferenceError: this is not defined
B. SyntaxError: Unexpected token
C. No error, works fine
D. TypeError: Cannot read property 'next' of null
Solution
Step 1: Analyze empty queue condition
When queue is empty, this.tail is null or undefined.
Step 2: Identify error line
Code tries this.tail.next = node; but this.tail is null, causing TypeError.
Final Answer:
TypeError: Cannot read property 'next' of null -> Option D
Quick Check:
Accessing next on null causes TypeError [OK]
Hint: Check if tail exists before accessing next [OK]
Common Mistakes:
Ignoring null check for tail
Assuming no error on empty queue
Confusing this.head and this.tail usage
5. You want to implement enqueue in a linked list queue that keeps track of both head and tail. Which of these code snippets correctly enqueues a new value val?