Online Coding Lessons Data Structures and Algorithms 2 — Questions and Answers
Question 1: What is a linked list?
- An array with a fixed size
- A data structure where each node points to the next node (Correct answer)
- A sorted array
- A tree with two branches
Correct answer: A data structure where each node points to the next node
A linked list is a data structure where each element (node) contains data and a pointer/reference to the next node in the sequence.
Question 2: What is the main advantage of binary search over linear search?
- It works on unsorted arrays
- It is faster, with O(log n) complexity on sorted arrays (Correct answer)
- It uses less memory
- It works on strings only
Correct answer: It is faster, with O(log n) complexity on sorted arrays
Binary search has O(log n) time complexity on sorted arrays, making it much faster than linear search's O(n) for large datasets.
Question 3: What is a tree data structure?
- A list of elements in sorted order
- A hierarchical structure with a root node and child nodes (Correct answer)
- A circular linked list
- A hash table with keys
Correct answer: A hierarchical structure with a root node and child nodes
A tree is a non-linear hierarchical data structure with a root node at the top and child nodes branching below it.
Question 4: What does 'O(1)' mean in Big O notation?
- The algorithm takes no time
- The algorithm has constant time complexity regardless of input size (Correct answer)
- The algorithm takes n steps
- The algorithm uses one byte of memory
Correct answer: The algorithm has constant time complexity regardless of input size
O(1) means constant time complexity — the algorithm takes the same amount of time regardless of how large the input is.
Question 5: Which data structure is best suited for implementing a 'back' button in a browser?
- Queue
- Stack (Correct answer)
- Array
- Linked List
Correct answer: Stack
A stack is ideal for a browser's back button because visited pages are added to the top, and clicking 'back' pops the most recent page off the stack (LIFO).
Question 6: What is recursion in programming?
- A loop that runs exactly 10 times
- A function that calls itself to solve a smaller version of the same problem (Correct answer)
- A type of array sorting
- A method for searching data
Correct answer: A function that calls itself to solve a smaller version of the same problem
Recursion is a programming technique where a function calls itself with a modified input to solve a problem by breaking it into smaller subproblems.
What is a linked list?