A. pop() removes last element, should use items[0]
B. pop() is correct for peek operation
C. items array should be private
D. peek method should return void
Solution
Step 1: Understand pop() behavior
pop() removes and returns the last element, not the front.
Step 2: Correct way to peek front
To peek front, return this.items[0] without removing it.
Final Answer:
pop() removes last element, should use items[0] -> Option A
Quick Check:
pop() ≠ peek front [OK]
Hint: pop() removes last, peek front is items[0] [OK]
Common Mistakes:
Using pop() for peek
Not returning front element correctly
Ignoring array indexing
5. You have a queue implemented as a linked list with nodes having value and next. How do you peek the front element safely when the queue might be empty?
hard
A. Return head.value directly without checks
B. Check if head is null; if not, return head.value, else return null
C. Traverse to the last node and return its value
D. Remove the head node and return its value
Solution
Step 1: Understand linked list queue front
The front element is at the head node of the linked list.
Step 2: Handle empty queue case
If head is null, queue is empty, so return null or undefined safely.
Step 3: Return front value safely
If head exists, return head.value without removing the node.
Final Answer:
Check if head is null; if not, return head.value, else return null -> Option B
Quick Check:
Peek front safely = check head then return value [OK]
Hint: Check head null before peek to avoid errors [OK]
Common Mistakes:
Not checking for empty queue
Returning value without null check
Removing node instead of peeking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.