Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Search for a Value in Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of searching for value 3 in this linked list?
Consider the linked list created by inserting values 1, 2, 3, 4 in order. What will be the output of searching for value 3?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insert(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  search(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        return true;
      }
      current = current.next;
    }
    return false;
  }
}

const list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
console.log(list.search(3));
Aundefined
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
Think about how the search method checks each node's value.
❓ Predict Output
intermediate
2:00remaining
What is the output when searching for a value not in the list?
Given the linked list with values 5, 10, 15, what is the output of searching for value 7?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insert(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  search(value) {
    let current = this.head;
    while (current) {
      if (current.value === value) {
        return true;
      }
      current = current.next;
    }
    return false;
  }
}

const list = new LinkedList();
list.insert(5);
list.insert(10);
list.insert(15);
console.log(list.search(7));
Afalse
Bnull
CReferenceError
Dtrue
Attempts:
2 left
💡 Hint
If the value is not found, what does the search method return?
🔧 Debug
advanced
2:00remaining
Why does this search method cause an infinite loop?
Identify the bug causing an infinite loop in the search method below.
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insert(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  search(value) {
    let current = this.head;
    while (current && current.value !== value) {
      current = current.next;
    }
    return current !== null;
  }
}
AThe loop condition does not check if current is null before accessing current.value
BThe insert method does not add nodes correctly
CThe Node constructor is missing the next property
DThe search method returns the wrong value
Attempts:
2 left
💡 Hint
Consider what happens when the value is not found in the list.
🚀 Application
advanced
2:00remaining
How many nodes are traversed when searching for value 4 in this list?
Given a linked list with nodes containing values 1 -> 2 -> 3 -> 4 -> 5, how many nodes does the search method check when searching for value 4?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insert(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  search(value) {
    let current = this.head;
    let count = 0;
    while (current) {
      count++;
      if (current.value === value) {
        break;
      }
      current = current.next;
    }
    return count;
  }
}

const list = new LinkedList();
list.insert(1);
list.insert(2);
list.insert(3);
list.insert(4);
list.insert(5);
console.log(list.search(4));
A3
B2
C5
D4
Attempts:
2 left
💡 Hint
Count nodes checked until the value is found.
🧠 Conceptual
expert
2:00remaining
What is the time complexity of searching for a value in a singly linked list?
Consider a singly linked list with n nodes. What is the worst-case time complexity of searching for a value in this list?
AO(1)
BO(n)
CO(log n)
DO(n log n)
Attempts:
2 left
💡 Hint
Think about how many nodes you might need to check in the worst case.

Practice

(1/5)
1. What is the main purpose of searching for a value in a linked list?
easy
A. To find if the value exists in the list
B. To add a new node at the end
C. To delete the first node
D. To reverse the linked list

Solution

  1. Step 1: Understand the search operation

    Searching in a linked list means checking each node to see if it contains the target value.
  2. Step 2: Identify the goal of searching

    The goal is to find whether the value exists anywhere in the list, not to modify the list.
  3. Final Answer:

    To find if the value exists in the list -> Option A
  4. Quick Check:

    Search means find value presence = C [OK]
Hint: Search means check nodes for value presence [OK]
Common Mistakes:
  • Confusing search with insertion or deletion
  • Thinking search changes the list
  • Assuming search reverses the list
2. Which of the following is the correct way to start searching a linked list in JavaScript if head is the first node?
easy
A. let current = null;
B. let current = head;
C. let current = head.next.next;
D. let current = head.value;

Solution

  1. Step 1: Understand the starting point of search

    Search starts from the first node, so we assign the first node to a variable to traverse.
  2. Step 2: Choose the correct assignment

    Assigning head to current lets us start from the first node and move forward.
  3. Final Answer:

    let current = head; -> Option B
  4. Quick Check:

    Start search at head node = B [OK]
Hint: Start search at head node with let current = head [OK]
Common Mistakes:
  • Starting at null means no nodes to check
  • Skipping nodes by starting at head.next.next
  • Assigning value instead of node reference
3. What will be the output of the following code snippet?
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);

function search(head, target) {
  let current = head;
  while (current !== null) {
    if (current.value === target) {
      return true;
    }
    current = current.next;
  }
  return false;
}

console.log(search(head, 10));
console.log(search(head, 20));
medium
A. true\ntrue
B. false\ntrue
C. true\nfalse
D. false\nfalse

Solution

  1. Step 1: Trace search for value 10

    Nodes are 5 -> 10 -> 15. The search finds 10 in the second node and returns true.
  2. Step 2: Trace search for value 20

    20 is not in the list, so the loop ends and returns false.
  3. Final Answer:

    true\nfalse -> Option C
  4. Quick Check:

    10 found = true, 20 not found = false [OK]
Hint: Return true if found, else false after full traversal [OK]
Common Mistakes:
  • Returning false too early
  • Not updating current to next node
  • Confusing value with node reference
4. Identify the error in the following search function for a linked list:
function search(head, target) {
  let current = head;
  while (current.next !== null) {
    if (current.value === target) {
      return true;
    }
    current = current.next;
  }
  return false;
}
medium
A. It does not check the last node's value
B. It uses incorrect variable names
C. It returns true too early
D. It never updates current

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while current.next !== null, so it stops before the last node.
  2. Step 2: Check if last node is checked

    The last node's value is never checked because loop ends early, so search may miss target at last node.
  3. Final Answer:

    It does not check the last node's value -> Option A
  4. Quick Check:

    Loop misses last node check = D [OK]
Hint: Loop until current is null, not current.next [OK]
Common Mistakes:
  • Using current.next !== null instead of current !== null
  • Forgetting to check last node
  • Assuming loop covers all nodes
5. Given a linked list where each node contains a number, how would you modify the search function to return the position (1-based) of the first occurrence of a target value, or -1 if not found?
hard
A. Reverse the list before searching for the value
B. Return true if value found; else false
C. Count total nodes first; then search for value
D. Initialize a position counter at 1; traverse nodes; return position if value matches; else -1

Solution

  1. Step 1: Add a position counter starting at 1

    Initialize a variable to track the current node's position as you traverse the list.
  2. Step 2: Traverse and compare values

    For each node, check if its value matches the target. If yes, return the current position.
  3. Step 3: Return -1 if not found

    If traversal ends without finding the target, return -1 to indicate absence.
  4. Final Answer:

    Initialize a position counter at 1; traverse nodes; return position if value matches; else -1 -> Option D
  5. Quick Check:

    Position tracking with traversal = A [OK]
Hint: Track position while searching; return position or -1 [OK]
Common Mistakes:
  • Returning boolean instead of position
  • Not updating position counter
  • Reversing list unnecessarily
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