Sharpen your understanding of core data structures and basic algorithms with this quiz designed to reinforce concepts like arrays, stacks, queues, sorting, and searching. Ideal for reinforcing foundational programming knowledge and quick algorithmic thinking.
Which data structure provides constant-time access to an element using its index, for example: access element 5 in a list of numbers?
Explanation: Arrays provide constant-time access (O(1)) to elements using their index, such as getting the 5th item directly. Trees involve hierarchical data and accessing a specific node may take longer. A stack restricts access to only the top element and does not allow indexing. Linkedlist requires traversal from the head for each position. Therefore, only 'Array' offers direct access via indices.
What is the term for adding an item to the end of a queue, such as joining a line at a ticket counter?
Explanation: 'Enqueue' refers to adding an item to the end (rear) of a queue, much like joining a line. 'Push' is used for adding items to a stack. 'Pop' removes an item from a stack or queue but does not add. 'InsertTop' is not standard terminology in queues. Thus, 'Enqueue' is the correct queue operation.
If you add books to a stack one by one and remove the top book each time, which principle is being used?
Explanation: Stack operations follow the Last-In, First-Out (LIFO) principle, so the last item added is the first one removed. 'First-In, First-Out' describes queues, not stacks. 'First-In, Last-Out' is incorrect, and 'Random Access' does not describe the order of removal or addition in stacks. Therefore, 'Last-In, First-Out' best fits the stack concept.
To correctly use binary search on a list of numbers, what property must the list have?
Explanation: Binary search requires that the list be in sorted order so it can efficiently divide the search range each step. Having unique values is not required, though it may affect the result if searching for duplicates. 'Stack structure' and 'Linked nodes' describe different data structures and do not ensure the list is searchable via binary search. Only sorted order is required.
When searching for a value in an unsorted list, which search method checks every element one by one?
Explanation: Linear search checks each element sequentially, making it suitable for unsorted lists. 'Binary search' can only be used on sorted data. 'Fast find' is not a standard term for this operation, and 'hashing' involves using a function to index data rather than sequential search. Thus, for unsorted data, 'Linear search' is correct.
Which simple algorithm repeatedly compares adjacent elements and swaps them to sort a small list like [4, 2, 1, 3]?
Explanation: Bubble sort works by comparing and swapping adjacent elements, 'bubbling' larger values toward the end. 'Merge sort' involves dividing the list, and 'Selection sort' finds the minimum element to place in order. 'Inserting sort' is a typo, intended to refer to 'Insertion sort,' but for adjacent comparisons and swaps, 'Bubble sort' is correct.
Which data structure consists of nodes, each pointing to the next, forming a chain like 1 → 2 → 3?
Explanation: A linked list is composed of nodes where each node points to the next, forming a sequence such as 1 → 2 → 3. 'HashSet' is an unordered collection and does not form a chain. 'Array index' refers to indexed access, not pointer-based chains. 'Tree map' indicates a keyed, hierarchical structure. Only 'Linked list' matches this sequential, pointer-based definition.
When functions call each other in a program, which data structure is typically used to keep track of what to return to?
Explanation: A stack is used during function calls to manage return addresses, so each call is tracked in a Last-In, First-Out way. 'Queue' is not suitable for function returns since order matters. 'Heap' deals with memory allocation, not function call tracking. 'Graph' represents relationships, not call order. Thus, only 'Stack' properly tracks function calls.
Which data structure models people waiting in a single line at a bus stop, with the first person served first?
Explanation: Queues follow the First-In, First-Out (FIFO) ordering, just like a real-world line where the first person to enter the line is the first to leave. 'Stack' is Last-In, First-Out, which does not match the scenario. 'Priority stack' is not a standard term, and 'Circular list' does not model waiting lines in this way. 'Queue' best fits the bus line scenario.
Which data structure is most suitable for quickly looking up a phone number based on someone's name?
Explanation: Hash tables offer efficient key-based lookups, making them excellent for quickly finding a value like a phone number by name. 'Array' would require scanning elements or knowing the index, which is less efficient. 'Queue' stores items in sequence, not by key. 'Binary tree' allows fast lookups but usually requires sorted keys, and may be less efficient than hashing for this case. Thus, 'Hash table' is most appropriate.
What is the simplest way to find the largest value in an unsorted array such as [17, 4, 31, 9]?
Explanation: The simplest way to find the largest number in an unsorted array is to scan each element and keep track of the maximum seen so far. Sorting would require extra time and is unnecessary for just finding the largest value. Binary search only works for sorted data and is not used to find extremes. Deleting elements does not solve the problem. Therefore, a linear scan is correct.
Which data structure represents hierarchical relationships such as folders inside other folders?
Explanation: A tree structure models hierarchy, like folders within folders, with parent and child nodes. 'Array' is a flat collection of items and does not show hierarchy. 'Stack' is linear and cannot represent parent-child relationships. 'Hash queue' is not a recognized data structure. Only 'Tree' naturally fits hierarchical data.
In which data structure is it fast to add an item at the beginning, such as inserting a new first element?
Explanation: For a linked list, inserting at the beginning is fast, requiring just pointer updates. In arrays, inserting at the start requires shifting all elements, making it slower. 'HashSet' does not maintain order, and 'Priority list' is not a specific structure, possibly confusing with 'priority queue.' Thus, 'Linked list' allows efficient head insertion.
If an operation gets slower in direct proportion to the number of items, what is the time complexity called?
Explanation: O(n) time complexity means the operation scales linearly with the size of the data, becoming slower as more items are involved. O(1) means constant time, unaffected by input size. O(n^2) is quadratic time, where time increases much faster. O(logn) is logarithmic time, which grows much more slowly. Among these, O(n) matches the description.
Which pair of operations is most closely associated with the stack data structure?
Explanation: Stack supports two primary operations: push (add an item) and pop (remove the most recent item). 'Insert and dequeue' mixes operations from different structures. 'Enqueue and remove' generally apply to queues. 'Sort and find' are unrelated to stack operation. Only 'Push and pop' defines stack behavior.
How do you visit each item in a singly linked list from start to finish?
Explanation: You traverse a singly linked list by starting at the head and following each node's next pointer until the end. 'Scan random elements' is not possible without indexes. 'Use binary search' requires sorted arrays, not lists. 'Access last element first' cannot be done efficiently in a singly linked list. Therefore, following 'next' pointers is the correct method.