What does 'Traversal Forward' mean in a data structure like a linked list?
Traversal Forward means moving through the data structure from the first element to the last, visiting each element in order.
Click to reveal answer
beginner
What is 'Traversal Backward' in the context of a doubly linked list?
Traversal Backward means moving through the data structure from the last element to the first, visiting each element in reverse order.
Click to reveal answer
intermediate
Why can we only traverse backward in a doubly linked list and not in a singly linked list?
Because a doubly linked list has pointers to both the next and previous elements, allowing backward movement. A singly linked list only has pointers to the next element, so backward traversal is not possible.
Click to reveal answer
beginner
In JavaScript, how would you start traversing a singly linked list forward?
Start from the head node and repeatedly move to the next node until you reach null, visiting each node along the way.
Click to reveal answer
intermediate
What is the main benefit of being able to traverse backward in a data structure?
It allows you to visit elements in reverse order easily, which can be useful for undo operations, reverse iteration, or certain algorithms.
Click to reveal answer
Which data structure allows traversal both forward and backward?
AQueue
BSingly linked list
CStack
DDoubly linked list
✗ Incorrect
Only doubly linked lists have pointers to both next and previous nodes, enabling forward and backward traversal.
In forward traversal of a singly linked list, where do you start?
AHead node
BTail node
CMiddle node
DAny random node
✗ Incorrect
Traversal starts at the head node and moves forward through next pointers.
What happens when you try to traverse backward in a singly linked list?
AYou can move to the previous node easily
BYou get an error
CYou cannot move backward because there is no previous pointer
DYou can move backward only if you store nodes in an array
✗ Incorrect
Singly linked lists do not have previous pointers, so backward traversal is not possible.
Which pointer is used to move forward in a doubly linked list?
Aprev
Bnext
Chead
Dtail
✗ Incorrect
The 'next' pointer moves forward to the next node.
Why might backward traversal be useful?
ATo visit elements in reverse order
BTo speed up forward traversal
CTo delete nodes faster
DTo avoid using pointers
✗ Incorrect
Backward traversal lets you visit elements from last to first, useful for reverse operations.
Explain how forward and backward traversal work in doubly linked lists.
Think about how the pointers connect nodes in both directions.
You got /5 concepts.
Describe why singly linked lists do not support backward traversal and how this affects their use.
Focus on the pointer structure and its impact.
You got /5 concepts.
Practice
(1/5)
1. What does it mean to traverse a linked list forward?
easy
A. Visiting nodes randomly without order
B. Visiting nodes from the first node to the last node in order
C. Skipping every other node while visiting
D. Visiting nodes from the last node to the first node in reverse order
Solution
Step 1: Understand forward traversal
Forward traversal means starting at the first node (head) and moving to the next node repeatedly until the end.
Step 2: Compare with other options
Backward traversal is the opposite, from last to first. Skipping or random visiting is not traversal.
Final Answer:
Visiting nodes from the first node to the last node in order -> Option B
Quick Check:
Forward traversal = first to last [OK]
Hint: Forward means start to end in order [OK]
Common Mistakes:
Confusing forward with backward traversal
Thinking traversal skips nodes
Assuming traversal is random
2. Which of the following is the correct syntax to traverse an array arr forward in JavaScript?
easy
A. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
B. for (let i = arr.length; i >= 0; i--) { console.log(arr[i]); }
C. for (let i = 1; i <= arr.length; i++) { console.log(arr[i]); }
D. for (let i = arr.length - 1; i > 0; i--) { console.log(arr[i]); }
Solution
Step 1: Identify forward traversal syntax
Forward traversal starts at index 0 and goes up to index arr.length - 1, so i from 0 to less than arr.length.
Step 2: Check each option
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } uses i = 0 to i < arr.length, correct. for (let i = arr.length; i >= 0; i--) { console.log(arr[i]); } and D count backward. for (let i = 1; i <= arr.length; i++) { console.log(arr[i]); } starts at 1 and accesses arr[arr.length], which is undefined.
Final Answer:
for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } -> Option A
Quick Check:
Forward array loop = i from 0 to length-1 [OK]
Hint: Start index 0, end before length for forward loops [OK]
Common Mistakes:
Starting loop at 1 instead of 0
Using <= instead of < in loop condition
Looping backward for forward traversal
3. What will be the output of the following code?
const arr = [10, 20, 30];
for (let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}
medium
A. 10 20 30
B. undefined undefined undefined
C. 30 20 10
D. SyntaxError
Solution
Step 1: Analyze loop indices
The loop starts at i = arr.length - 1 = 2, then decrements i until 0, so indices 2, 1, 0.
Step 2: Print elements in reverse order
arr[2] = 30, arr[1] = 20, arr[0] = 10, so output is 30 20 10.
Final Answer:
30 20 10 -> Option C
Quick Check:
Backward loop prints elements in reverse [OK]
Hint: Loop backward from length-1 to 0 for reverse output [OK]
Common Mistakes:
Confusing forward and backward indices
Expecting output in original order
Off-by-one errors in loop condition
4. Identify the error in the following backward traversal code for an array nums:
for (let i = nums.length; i >= 0; i--) {
console.log(nums[i]);
}
medium
A. No error, code is correct
B. Loop should increment i instead of decrement
C. Loop condition should be i <= nums.length
D. Loop should start at nums.length - 1 and decrement i
Solution
Step 1: Check loop start and direction
Array indices go from 0 to nums.length - 1. Starting at nums.length is out of bounds.
Step 2: Correct loop start and decrement
Loop should start at nums.length - 1 and decrement i to 0 to traverse backward correctly.
Final Answer:
Loop should start at nums.length - 1 and decrement i -> Option D
Quick Check:
Backward loop start = length-1, decrement i [OK]
Hint: Start backward loop at length-1, not length [OK]
Common Mistakes:
Starting loop at length causing undefined access
Incrementing index in backward loop
Wrong loop condition allowing infinite loop
5. Given a doubly linked list, which traversal method allows visiting nodes both forward and backward efficiently?
hard
A. Using next pointers for forward and prev pointers for backward traversal
B. Using only next pointers for both directions
C. Using only prev pointers for both directions
D. Traversing randomly without pointers
Solution
Step 1: Understand doubly linked list structure
Doubly linked lists have two pointers per node: next (forward) and prev (backward).