Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Traversal Forward and Backward in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Traversal Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Forward Traversal in a Doubly Linked List
What is the printed output of the following JavaScript code that traverses a doubly linked list forward?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  traverseForward() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new DoublyLinkedList();
list.append(10);
list.append(20);
list.append(30);
list.traverseForward();
A10 -> 20 -> 30 -> null
Bnull -> 10 -> 20 -> 30
C30 -> 20 -> 10 -> null
D10 -> 20 -> null
Attempts:
2 left
💡 Hint
Think about how the traversal moves from head to tail using the next pointer.
❓ Predict Output
intermediate
2:00remaining
Output of Backward Traversal in a Doubly Linked List
What is the printed output of the following JavaScript code that traverses a doubly linked list backward?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  traverseBackward() {
    let current = this.tail;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.prev;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new DoublyLinkedList();
list.append(5);
list.append(15);
list.append(25);
list.traverseBackward();
A15 -> 25 -> null
B5 -> 15 -> 25 -> null
Cnull -> 25 -> 15 -> 5
D25 -> 15 -> 5 -> null
Attempts:
2 left
💡 Hint
Backward traversal starts from the tail and moves using the prev pointer.
🧠 Conceptual
advanced
1:00remaining
Understanding Traversal Pointers in Doubly Linked Lists
In a doubly linked list, which pointer is used to move backward during traversal?
Anext pointer
Bprev pointer
Chead pointer
Dtail pointer
Attempts:
2 left
💡 Hint
Backward means going to the previous node.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Backward Traversal Code
What error will occur when running the following backward traversal code snippet?
DSA Javascript
traverseBackward() {
  let current = this.head;
  let result = '';
  while (current) {
    result += current.value + ' -> ';
    current = current.prev;
  }
  result += 'null';
  console.log(result);
}
ANo output, infinite loop
BTypeError because current.prev is undefined at some point
CCorrect output showing backward traversal
DSyntaxError due to missing semicolon
Attempts:
2 left
💡 Hint
Check where traversal starts and which pointer is used.
🚀 Application
expert
3:00remaining
Resulting List After Mixed Forward and Backward Traversals
Given a doubly linked list with nodes 1, 2, 3 appended in order, what is the printed output after the following operations? 1. Traverse forward and print values. 2. Traverse backward and print values. 3. Append 4. 4. Traverse backward and print values again.
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  traverseForward() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }

  traverseBackward() {
    let current = this.tail;
    let result = '';
    while (current) {
      result += current.value + ' -> ';
      current = current.prev;
    }
    result += 'null';
    console.log(result);
  }
}

const list = new DoublyLinkedList();
list.append(1);
list.append(2);
list.append(3);
list.traverseForward();
list.traverseBackward();
list.append(4);
list.traverseBackward();
A
null
null
4 -> 3 -> 2 -> 1 -> null
B
1 -> 2 -> 3 -> null
3 -> 2 -> 1 -> null
1 -> 2 -> 3 -> 4 -> null
C
1 -> 2 -> 3 -> null
3 -> 2 -> 1 -> null
4 -> 3 -> 2 -> 1 -> null
D
1 -> 2 -> 3 -> null
1 -> 2 -> 3 -> null
4 -> 3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember that append adds to the tail and backward traversal starts from tail.

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

  1. Step 1: Understand forward traversal

    Forward traversal means starting at the first node (head) and moving to the next node repeatedly until the end.
  2. Step 2: Compare with other options

    Backward traversal is the opposite, from last to first. Skipping or random visiting is not traversal.
  3. Final Answer:

    Visiting nodes from the first node to the last node in order -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    for (let i = 0; i < arr.length; i++) { console.log(arr[i]); } -> Option A
  4. 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

  1. Step 1: Analyze loop indices

    The loop starts at i = arr.length - 1 = 2, then decrements i until 0, so indices 2, 1, 0.
  2. Step 2: Print elements in reverse order

    arr[2] = 30, arr[1] = 20, arr[0] = 10, so output is 30 20 10.
  3. Final Answer:

    30 20 10 -> Option C
  4. 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

  1. Step 1: Check loop start and direction

    Array indices go from 0 to nums.length - 1. Starting at nums.length is out of bounds.
  2. Step 2: Correct loop start and decrement

    Loop should start at nums.length - 1 and decrement i to 0 to traverse backward correctly.
  3. Final Answer:

    Loop should start at nums.length - 1 and decrement i -> Option D
  4. 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

  1. Step 1: Understand doubly linked list structure

    Doubly linked lists have two pointers per node: next (forward) and prev (backward).
  2. Step 2: Use pointers for traversal

    Forward traversal uses next pointers; backward traversal uses prev pointers efficiently.
  3. Final Answer:

    Using next pointers for forward and prev pointers for backward traversal -> Option A
  4. Quick Check:

    Doubly linked list uses next and prev pointers [OK]
Hint: Doubly linked list has next and prev pointers [OK]
Common Mistakes:
  • Assuming single pointer can traverse both ways
  • Ignoring prev pointers in backward traversal
  • Thinking traversal can be random
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard