Data Structures & AlgorithmsEasyAccenture · Cognizant pattern
Only the head pointer of a singly linked list of n nodes is known (no length is stored). What is the time to reach its middle node?
- A. O(1)
- B. O(n)
- C. O(log n)
- D. O(√n)
Correct answer: B. O(n)
A linked list has no index arithmetic: you must walk node by node (or run slow/fast pointers), touching about n/2 nodes – O(n). O(1) access to the middle is an array property; binary search on a linked list is not O(log n) for the same reason.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- An empty deque performs pushfront(1), pushfront(2), pushfront(3) and then popback(). Which value is removed?
- A sorted array holds n elements and has spare capacity. What is the worst-case time to insert a new element so that the array stays sorted?
- Counting sort on [2, 0, 2, 1, 0, 2] (values 0–2) builds count[v] and then turns it into cumulative counts, count[v] = number of elements ≤…
- Two pointers start at both ends of the sorted array [1, 3, 4, 6, 8, 11] to find pairs of DIFFERENT positions with sum 12 (move left if the…
- A hash map starts with capacity 16 and doubles its capacity whenever the number of keys EXCEEDS 0.75 × capacity. What is its capacity…
- Bubble sort (each pass compares adjacent pairs left to right and swaps if out of order) runs on [6, 2, 9, 1, 5]. What is the array after…