1. Home
  2. Explore
  3. Computer Science
  4. Data Structures Fundamentals

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Data Structures Fundamentals

  • 150Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Foundations and Complexity

Which description best captures the purpose of a data structure?

150 questions

  1. 1

    Which description best captures the purpose of a data structure?

    1. A

      It specifies the exact programming language an algorithm must use

    2. B

      It converts every kind of data into a sorted sequence

    3. C

      It guarantees that every algorithm uses the least possible memory

    4. D

      It organizes data so the data can be accessed, queried, or updated efficiently

    Correct answer
    D

    It organizes data so the data can be accessed, queried, or updated efficiently

    Explanation

    A data structure organizes data to make operations such as access, querying, and updating efficient.

  2. 2

    Why can choosing an appropriate data structure improve a software product?

    1. A

      It allows every operation to run in constant time

    2. B

      It eliminates the need to analyze an algorithm's time and space use

    3. C

      It can support faster algorithms, natural data management, and clearer code

    4. D

      It guarantees that a program will never encounter invalid input

    Correct answer
    C

    It can support faster algorithms, natural data management, and clearer code

    Explanation

    Appropriate data structures can make algorithms faster, data easier to manage, and code cleaner and easier to understand.

  3. 3

    What does an abstract data type specify, compared with a data structure?

    1. A

      The fastest possible algorithm for each supported operation

    2. B

      The behavior and interface that an implementation must provide

    3. C

      The particular values that must be stored in the structure

    4. D

      The memory layout and programming language of every implementation

    Correct answer
    B

    The behavior and interface that an implementation must provide

    Explanation

    An abstract data type defines the operations and behavior exposed to users, not the details of how those operations are implemented.

  4. 4

    A list can be implemented by either a dynamic array or a linked list. What does this illustrate?

    1. A

      One abstract data type can have multiple underlying data structures

    2. B

      Different implementations must expose different list behavior

    3. C

      A list is itself always implemented as a linked list

    4. D

      An abstract data type determines the memory layout of its implementation

    Correct answer
    A

    One abstract data type can have multiple underlying data structures

    Explanation

    A list is an abstract data type; a dynamic array and a linked list are possible implementations that provide list operations.

  5. 5

    Why can a queue implemented using stacks still qualify as a queue?

    1. A

      Any structure implemented with stacks automatically runs in constant time

    2. B

      A queue's abstract interface requires it to use two stacks

    3. C

      Stacks and queues have identical insertion and removal behavior

    4. D

      It can provide queue behavior even if its internal implementation is different

    Correct answer
    D

    It can provide queue behavior even if its internal implementation is different

    Explanation

    An abstract data type specifies behavior, not implementation. A stack-based implementation can provide queue operations, even if it is not the most efficient choice.

  6. 6

    What does Big O notation describe in this material?

    1. A

      The exact runtime of an algorithm on every computer

    2. B

      The number of lines of code used to implement an algorithm

    3. C

      The average runtime for inputs of a fixed, small size

    4. D

      An algorithm's asymptotic worst-case growth in time or space

    Correct answer
    D

    An algorithm's asymptotic worst-case growth in time or space

    Explanation

    Big O is used to describe worst-case growth, particularly as input size becomes arbitrarily large.

  7. 7

    For a search through an unordered list, which input arrangement gives the worst case?

    1. A

      The desired item is found after checking only a constant number of elements

    2. B

      The desired item is the final element, or is absent and every element must be checked

    3. C

      The list contains repeated values near its beginning

    4. D

      The desired item is the first element

    Correct answer
    B

    The desired item is the final element, or is absent and every element must be checked

    Explanation

    In the worst case, a linear search must traverse every element before finding the target at the end, or determine that the target is absent.

  8. 8

    Why are constant factors and lower-order terms usually omitted in Big O analysis?

    1. A

      They do not determine the dominant growth as input size becomes arbitrarily large

    2. B

      They are always zero for sufficiently large inputs

    3. C

      They can be removed from the algorithm without changing its output

    4. D

      They have no effect on real program runtimes

    Correct answer
    A

    They do not determine the dominant growth as input size becomes arbitrarily large

    Explanation

    Asymptotic analysis focuses on dominant growth for large inputs. Constants may still matter substantially in real-world runtimes.

  9. 9

    What is the Big O class of 7log⁡(n3)+15n2+2n3+87\log(n^3)+15n^2+2n^3+87log(n3)+15n2+2n3+8?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(2n)O(2^n)O(2n)

    3. C

      O(n3)O(n^3)O(n3)

    4. D

      O(log⁡n)O(\log n)O(logn)

    Correct answer
    C

    O(n3)O(n^3)O(n3)

    Explanation

    The cubic term grows faster than the other terms, so it determines the asymptotic class: O(n3)O(n^3)O(n3).

  10. 10

    A loop runs a fixed number of times regardless of the input size nnn. What is its time complexity?

    1. A

      O(log⁡n)O(\log n)O(logn)

    2. B

      O(1)O(1)O(1)

    3. C

      O(n2)O(n^2)O(n2)

    4. D

      O(n)O(n)O(n)

    Correct answer
    B

    O(1)O(1)O(1)

    Explanation

    Because the loop's work does not depend on nnn, its asymptotic running time is constant.

  11. 11

    A loop processes every input element, but advances its counter by three each time. What is its Big O complexity?

    1. A

      O(n)O(n)O(n)

    2. B

      O(n2)O(n^2)O(n2)

    3. C

      O(log⁡n)O(\log n)O(logn)

    4. D

      O(n/3)O(n/3)O(n/3) as a distinct asymptotic class

    Correct answer
    A

    O(n)O(n)O(n)

    Explanation

    The loop performs about n/3n/3n/3 iterations, which is linear growth. Big O ignores the constant factor, so the result is O(n)O(n)O(n).

  12. 12

    An algorithm's inner loop performs n−in-in−i operations for each outer-loop index iii from 000 to n−1n-1n−1. Why is the total work quadratic?

    1. A

      The total work is proportional only to the final value of iii

    2. B

      The outer loop is ignored whenever an inner loop is present

    3. C

      The inner loop executes exactly once for the entire algorithm

    4. D

      The work sums to n+(n−1)+⋯+1=n(n+1)2n+(n-1)+\cdots+1=\frac{n(n+1)}{2}n+(n−1)+⋯+1=2n(n+1)​

    Correct answer
    D

    The work sums to n+(n−1)+⋯+1=n(n+1)2n+(n-1)+\cdots+1=\frac{n(n+1)}{2}n+(n−1)+⋯+1=2n(n+1)​

    Explanation

    Summing the decreasing inner-loop lengths gives n(n+1)2\frac{n(n+1)}{2}2n(n+1)​, whose dominant growth is quadratic.

  13. 13

    Why does binary search take logarithmic time in the worst case?

    1. A

      Each iteration discards half of the remaining search range

    2. B

      The algorithm performs a fixed number of comparisons for every input size

    3. C

      The array is sorted again after each comparison

    4. D

      Each iteration checks every element in the array

    Correct answer
    A

    Each iteration discards half of the remaining search range

    Explanation

    Repeatedly halving the search range leaves only a logarithmic number of iterations, giving O(log⁡n)O(\log n)O(logn) time.

  14. 14

    An outer loop performs nnn iterations, and each iteration runs two separate inner loops of 3n3n3n and 2n2n2n operations. What is the total Big O complexity?

    1. A

      O(n)O(n)O(n)

    2. B

      O(5n)O(5n)O(5n) as a distinct asymptotic class

    3. C

      O(n3)O(n^3)O(n3)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    D

    O(n2)O(n^2)O(n2)

    Explanation

    The inner loops contribute 3n+2n=5n3n+2n=5n3n+2n=5n work per outer iteration. Multiplying by nnn gives 5n25n^25n2, or O(n2)O(n^2)O(n2).

  15. 15

    A set of nnn items has how many possible subsets, and what time complexity does explicitly finding all of them imply?

    1. A

      n!n!n! subsets, implying factorial work

    2. B

      n2n^2n2 subsets, implying quadratic work

    3. C

      2n2^n2n subsets, implying exponential work

    4. D

      nlog⁡nn\log nnlogn subsets, implying linearithmic work

    Correct answer
    C

    2n2^n2n subsets, implying exponential work

    Explanation

    Each of the nnn items is either included or excluded, giving 2n2^n2n subsets and exponential work to enumerate them.

  16. 16

    Which pairing of tasks and time complexities is correct?

    1. A

      All string permutations: O(log⁡n)O(\log n)O(logn); merge sort: O(n!)O(n!)O(n!)

    2. B

      All string permutations: O(n2)O(n^2)O(n2); merge sort: O(1)O(1)O(1)

    3. C

      All string permutations: O(n!)O(n!)O(n!); merge sort: O(nlog⁡n)O(n\log n)O(nlogn)

    4. D

      All string permutations: O(nlog⁡n)O(n\log n)O(nlogn); merge sort: O(2n)O(2^n)O(2n)

    Correct answer
    C

    All string permutations: O(n!)O(n!)O(n!); merge sort: O(nlog⁡n)O(n\log n)O(nlogn)

    Explanation

    There are n!n!n! permutations of nnn distinct items, while merge sort has O(nlog⁡n)O(n\log n)O(nlogn) time complexity.

  17. 17

    What is a defining property of a static array?

    1. A

      It grows automatically whenever an element is appended

    2. B

      It has fixed length and stores indexable elements in contiguous memory

    3. C

      Its memory locations are deliberately separated by gaps

    4. D

      Its elements are accessed only by traversing pointers

    Correct answer
    B

    It has fixed length and stores indexable elements in contiguous memory

    Explanation

    A static array is a fixed-length, indexable container stored as a contiguous chunk of memory.

  18. 18

    In a zero-based array of length nnn, which index range is valid?

    1. A

      From 000 through nnn, inclusive

    2. B

      From 111 through nnn, inclusive

    3. C

      From 111 through n−1n-1n−1, inclusive

    4. D

      From 000 through n−1n-1n−1, inclusive

    Correct answer
    D

    From 000 through n−1n-1n−1, inclusive

    Explanation

    Zero-based indexing gives an array of length nnn valid indices from 000 to n−1n-1n−1.

  19. 19

    Why can an array retrieve an element at a specified index in constant time?

    1. A

      Its contiguous layout and indexability allow direct access to the requested slot

    2. B

      It scans from the first element until it reaches the requested index

    3. C

      It stores a separate linked list for every possible index

    4. D

      It sorts its elements before every retrieval

    Correct answer
    A

    Its contiguous layout and indexability allow direct access to the requested slot

    Explanation

    Array indexing directly identifies a slot in the contiguous memory block, so access takes O(1)O(1)O(1) time.

  20. 20

    Why can searching an array for an arbitrary value take linear time?

    1. A

      Every array access requires shifting the remaining elements

    2. B

      A search must resize the array before checking its contents

    3. C

      Arrays cannot compare the values they contain

    4. D

      The value may be absent or near the end, requiring a scan of all elements

    Correct answer
    D

    The value may be absent or near the end, requiring a scan of all elements

    Explanation

    Without an assumed search-organizing property, a search may need to inspect every element, giving O(n)O(n)O(n) worst-case time.

  21. 21

    Which example illustrates using an array as a buffer?

    1. A

      Reading a large file in small chunks that fit in memory

    2. B

      Keeping only the highest-priority item at the top

    3. C

      Merging disjoint groups after checking their representatives

    4. D

      Storing each item in a node that points to the next item

    Correct answer
    A

    Reading a large file in small chunks that fit in memory

    Explanation

    A buffer array can temporarily hold a small chunk of a large input or output stream at a time.

  22. 22

    Why are arrays useful as lookup tables?

    1. A

      They keep all values sorted without requiring an ordering

    2. B

      They automatically identify the key associated with every value

    3. C

      They make every search for an unknown position constant time

    4. D

      A known index or offset can be used to retrieve a stored value directly

    Correct answer
    D

    A known index or offset can be used to retrieve a stored value directly

    Explanation

    If the location associated with an item is known, array indexing retrieves it directly.

  23. 23

    How can an array help a function return several results in a language that allows only one return value?

    1. A

      Return each result in a separate function call automatically

    2. B

      Convert the results into different array indices without returning the array

    3. C

      Return a reference or pointer to an array containing the results

    4. D

      Use a static array to bypass the language's return-value rule

    Correct answer
    C

    Return a reference or pointer to an array containing the results

    Explanation

    A function can return a pointer or reference to an array that holds all the desired results.

  24. 24

    Why can appending to a dynamic array be constant time on average even though some appends trigger resizing?

    1. A

      Resizing never copies existing elements

    2. B

      Resizing happens infrequently, so its cost is spread across many inexpensive appends

    3. C

      Every append reserves a new array exactly one slot larger

    4. D

      The dynamic array stores elements outside of memory

    Correct answer
    B

    Resizing happens infrequently, so its cost is spread across many inexpensive appends

    Explanation

    When capacity is increased geometrically, costly copies occur infrequently, yielding amortized constant-time append.

  25. 25

    What typically happens when a dynamic array's internal capacity is exceeded?

    1. A

      The array becomes a linked list without moving any elements

    2. B

      A larger array is allocated, existing elements are copied, and the new element is added

    3. C

      All elements are shifted within the same fixed-size memory block

    4. D

      The last element is discarded to make room

    Correct answer
    B

    A larger array is allocated, existing elements are copied, and the new element is added

    Explanation

    A common dynamic-array implementation allocates a larger backing array, copies the old elements, and then adds the new element.

  26. 26

    Why are insertion or deletion at an arbitrary position in an array-based list typically linear time?

    1. A

      Other elements may need to shift, and resizing may require copying

    2. B

      The array must be searched even when the target index is already known

    3. C

      Every insertion or deletion requires sorting the entire array

    4. D

      The array has to convert from zero-based to one-based indexing

    Correct answer
    A

    Other elements may need to shift, and resizing may require copying

    Explanation

    Making room or closing a gap can require shifting elements; resizing may also require copying them.

  27. 27

    What is a singly linked list?

    1. A

      A fixed-length sequence of contiguous memory slots

    2. B

      A sequence in which each node points both forward and backward

    3. C

      A sequence of data-containing nodes in which each node points to the next

    4. D

      A tree in which each node has exactly two children

    Correct answer
    C

    A sequence of data-containing nodes in which each node points to the next

    Explanation

    A linked list consists of nodes holding data and pointers to other nodes; in a singly linked list, each node points to the next.

  28. 28

    What does the head reference provide in a linked list?

    1. A

      A guarantee that the last node has a previous pointer

    2. B

      A direct reference to every node in the list

    3. C

      A starting point from which the list can be traversed

    4. D

      A fixed index for every element

    Correct answer
    C

    A starting point from which the list can be traversed

    Explanation

    The head identifies the first node, providing a place to begin traversing the list.

  29. 29

    Which statement correctly contrasts singly and doubly linked lists?

    1. A

      Singly linked nodes point backward; doubly linked nodes point only forward

    2. B

      Singly linked nodes point to the next node; doubly linked nodes also point to the previous node

    3. C

      Singly linked lists have contiguous storage; doubly linked lists do not

    4. D

      Doubly linked lists cannot be traversed from the head

    Correct answer
    B

    Singly linked nodes point to the next node; doubly linked nodes also point to the previous node

    Explanation

    A doubly linked list stores both next and previous references, while a singly linked list stores only a next reference.

  30. 30

    What is a trade-off of using a doubly linked list instead of a singly linked list?

    1. A

      It uses more memory for an extra pointer per node but supports backward traversal

    2. B

      It guarantees constant-time searching but requires contiguous memory

    3. C

      It uses the same memory and supports fewer traversal directions

    4. D

      It uses less memory but cannot remove a known middle node

    Correct answer
    A

    It uses more memory for an extra pointer per node but supports backward traversal

    Explanation

    The previous pointer adds memory overhead, while allowing traversal backward and easier removal when a node reference is available.

  31. 31

    Why does removing a known node from the middle of a doubly linked list take constant time?

    1. A

      The list automatically sorts its nodes after each deletion

    2. B

      The list can locate the node by its array index

    3. C

      The node's previous and next references can be connected directly to each other

    4. D

      The node is copied to the end of the list before removal

    Correct answer
    C

    The node's previous and next references can be connected directly to each other

    Explanation

    Given the node reference, its neighboring nodes can be linked to each other directly, bypassing the removed node.

  32. 32

    Why does removing the tail of a singly linked list take linear time, even if a tail reference is maintained?

    1. A

      Every node must be copied into a new contiguous array

    2. B

      The new tail must be found by traversing from the head because backward links are absent

    3. C

      The tail cannot be accessed directly

    4. D

      The list must locate the previous node using binary search

    Correct answer
    B

    The new tail must be found by traversing from the head because backward links are absent

    Explanation

    A singly linked tail has no pointer to its predecessor, so the list must traverse from the head to find the new tail.

  33. 33

    What is the worst-case search time in a linked list?

    1. A

      O(nlog⁡n)O(n\log n)O(nlogn), because each node must be sorted

    2. B

      O(log⁡n)O(\log n)O(logn), because linked nodes form a balanced search tree

    3. C

      O(1)O(1)O(1), because each node stores its index

    4. D

      O(n)O(n)O(n), because the target may be absent or at the final node

    Correct answer
    D

    O(n)O(n)O(n), because the target may be absent or at the final node

    Explanation

    A linked-list search may have to visit all nnn nodes.

  34. 34

    What is the defining behavior of a stack?

    1. A

      It adds and removes elements at one end using last-in, first-out order

    2. B

      It removes the smallest element before all others

    3. C

      It adds at the back and removes from the front using first-in, first-out order

    4. D

      It allows direct access to any element by index

    Correct answer
    A

    It adds and removes elements at one end using last-in, first-out order

    Explanation

    A stack's push and pop operations occur at the top, producing last-in, first-out (LIFO) behavior.

  35. 35

    A stack initially has an item at its top. If two items are pushed and then one is popped, which item is removed?

    1. A

      The second item pushed

    2. B

      The first item pushed

    3. C

      The item with the smallest value

    4. D

      The item that was initially at the top

    Correct answer
    A

    The second item pushed

    Explanation

    A pop removes the most recently pushed item, which is the second item pushed in this sequence.

  36. 36

    How does a stack help a program support recursive function calls?

    1. A

      It sorts function calls by the length of their execution time

    2. B

      It stores active call frames so returning can resume the previous call

    3. C

      It stores all function results in an indexed array

    4. D

      It guarantees that recursive calls execute in parallel

    Correct answer
    B

    It stores active call frames so returning can resume the previous call

    Explanation

    The call stack tracks function frames; returning pops the current frame and resumes the preceding call.

  37. 37

    How can a stack determine whether brackets in a string are properly matched?

    1. A

      Push opening brackets; for each closing bracket, match and pop the top, then require the stack to be empty

    2. B

      Push closing brackets; for each opening bracket, remove the oldest item

    3. C

      Count all bracket characters and accept strings with equal opening and closing totals

    4. D

      Sort the brackets by type and compare the two halves

    Correct answer
    A

    Push opening brackets; for each closing bracket, match and pop the top, then require the stack to be empty

    Explanation

    The stack tracks unmatched opening brackets in nested order. Each closing bracket must match the top, and no unmatched openings may remain.

  38. 38

    Why is checking only whether a bracket string has equal numbers of opening and closing brackets insufficient?

    1. A

      Stacks cannot store more than one kind of bracket

    2. B

      Bracket matching depends only on the string's final character

    3. C

      A valid string must contain more opening brackets than closing brackets

    4. D

      Equal counts do not ensure that brackets are correctly nested or ordered

    Correct answer
    D

    Equal counts do not ensure that brackets are correctly nested or ordered

    Explanation

    A sequence can have equal counts but still contain mismatched types or an invalid closing order.

  39. 39

    How can the Tower of Hanoi pegs be modeled using stacks?

    1. A

      Each peg is a queue, since disks leave in the order they arrived

    2. B

      All disks are stored in a single array and accessed by arbitrary index

    3. C

      Each peg is a priority queue ordered by disk color

    4. D

      Each peg is a stack, since only the top disk is moved on or off a peg

    Correct answer
    D

    Each peg is a stack, since only the top disk is moved on or off a peg

    Explanation

    A move removes the top disk from one peg and places it on another, matching stack operations.

  40. 40

    What is the time complexity of push, pop, and peek when a stack is implemented with a linked list and a top reference?

    1. A

      O(1)O(1)O(1) for each operation

    2. B

      O(log⁡n)O(\log n)O(logn) for each operation

    3. C

      O(n)O(n)O(n) for each operation

    4. D

      O(n2)O(n^2)O(n2) for each operation

    Correct answer
    A

    O(1)O(1)O(1) for each operation

    Explanation

    The top is directly accessible, so adding, removing, or inspecting it takes constant time.

  41. 41

    What is a queue's standard removal and insertion order?

    1. A

      Insert and remove only at the back

    2. B

      Insert at the front and remove from the front

    3. C

      Insert at the back and remove from the front

    4. D

      Insert at the back and remove the highest-priority item

    Correct answer
    C

    Insert at the back and remove from the front

    Explanation

    A queue enqueues at the back and dequeues from the front, giving first-in, first-out behavior.

  42. 42

    A queue contains 55,−1,3355,-1,3355,−1,33 in that order. What remains at the front after two dequeue operations?

    1. A

      −1-1−1

    2. B

      The queue is empty

    3. C

      333333

    4. D

      555555

    Correct answer
    C

    333333

    Explanation

    The first dequeue removes 555555, and the second removes −1-1−1, leaving 333333 at the front.

  43. 43

    Why are enqueue, dequeue, and peek constant-time operations in a linked-list queue that maintains front and back references?

    1. A

      Each operation can access or update the relevant endpoint directly

    2. B

      Linked lists store every element at a fixed numerical index

    3. C

      The queue sorts its elements after every operation

    4. D

      The queue searches all nodes to find the front and back

    Correct answer
    A

    Each operation can access or update the relevant endpoint directly

    Explanation

    Maintaining endpoint references allows insertion at the back, removal at the front, and inspection of the front without scanning the list.

  44. 44

    Why does checking whether an arbitrary value is contained in a queue generally take linear time?

    1. A

      The value may require scanning the queue's elements to find

    2. B

      The queue must remove each element before checking it

    3. C

      A queue can only compare its front and back elements

    4. D

      The queue must sort itself before performing containment checks

    Correct answer
    A

    The value may require scanning the queue's elements to find

    Explanation

    A containment check may need to inspect every element, so its worst-case time is O(n)O(n)O(n).

  45. 45

    What is the core traversal order of breadth-first search?

    1. A

      Follow one path as deeply as possible before considering other neighbors

    2. B

      Visit nodes in increasing numerical order, regardless of edges

    3. C

      Visit a starting node's neighbors before expanding to more distant layers

    4. D

      Visit the largest-priority node before all its neighbors

    Correct answer
    C

    Visit a starting node's neighbors before expanding to more distant layers

    Explanation

    Breadth-first search expands outward by layers, visiting the current frontier's neighbors before moving farther away.

  46. 46

    Why does breadth-first search use a queue?

    1. A

      It processes discovered nodes in the order they enter the frontier

    2. B

      It always processes the most recently discovered node first

    3. C

      It selects the smallest-valued node in the graph

    4. D

      It provides direct access to every unvisited node

    Correct answer
    A

    It processes discovered nodes in the order they enter the frontier

    Explanation

    A FIFO queue processes earlier-discovered frontier nodes first, supporting breadth-first layer order.

  47. 47

    In breadth-first search, when should a newly discovered neighbor be marked visited?

    1. A

      Only if it has no neighbors of its own

    2. B

      After every node in the graph has been explored

    3. C

      Only after it has been removed from the queue twice

    4. D

      When it is first discovered and added to the queue

    Correct answer
    D

    When it is first discovered and added to the queue

    Explanation

    Marking a node visited when it is discovered prevents it from being repeatedly added to the queue.

  48. 48

    What does it mean if breadth-first search finishes without visiting a node in the graph?

    1. A

      The unvisited node must be the graph's root

    2. B

      That node is unreachable from the chosen starting node

    3. C

      The graph necessarily contains a cycle

    4. D

      The queue processed nodes in the wrong order

    Correct answer
    B

    That node is unreachable from the chosen starting node

    Explanation

    A traversal from one start reaches only nodes connected to that start by paths; an isolated or disconnected node remains unvisited.

  49. 49

    What property distinguishes a priority queue from an ordinary FIFO queue?

    1. A

      The oldest element always has the highest priority

    2. B

      Elements cannot be compared with one another

    3. C

      Elements can be inserted only at the front

    4. D

      Elements are removed according to priority rather than arrival order

    Correct answer
    D

    Elements are removed according to priority rather than arrival order

    Explanation

    A priority queue removes the element with the highest priority under its chosen ordering, not necessarily the one inserted first.

  50. 50

    What must be true of elements placed in a priority queue?

    1. A

      They must be comparable under an ordering used to determine priority

    2. B

      They must be inserted in sorted order

    3. C

      They must all be distinct integers

    4. D

      They must all have the same priority

    Correct answer
    A

    They must be comparable under an ordering used to determine priority

    Explanation

    The priority queue needs an ordering to determine which element has priority over another.

  51. 51

    In a min-priority queue, what does a poll operation return?

    1. A

      The smallest element currently in the queue

    2. B

      An arbitrary element from the queue

    3. C

      The most recently inserted element

    4. D

      The largest element currently in the queue

    Correct answer
    A

    The smallest element currently in the queue

    Explanation

    In a min-priority queue, smaller elements have higher priority and are removed first.

  52. 52

    Why does repeatedly polling a min-priority queue not always amount to sorting all values at the time of each poll?

    1. A

      A heap maintains enough ordering to find the current minimum without fully sorting all elements

    2. B

      A poll operation returns values in insertion order

    3. C

      A heap stores only the smallest value and discards the rest

    4. D

      A priority queue ignores the ordering of its elements

    Correct answer
    A

    A heap maintains enough ordering to find the current minimum without fully sorting all elements

    Explanation

    A heap's invariant makes its root the current minimum, avoiding a full sort before every poll.

  53. 53

    What is the heap invariant for a min heap?

    1. A

      Every node is smaller than every node in the next level

    2. B

      Every parent value is greater than or equal to its children's values

    3. C

      Every parent value is less than or equal to its children's values

    4. D

      All leaf values are equal

    Correct answer
    C

    Every parent value is less than or equal to its children's values

    Explanation

    In a min heap, each parent is no greater than its children, placing a minimum value at the root.

  54. 54

    What is the heap invariant for a max heap?

    1. A

      Every parent value is greater than or equal to its children's values

    2. B

      All leaf values are greater than the root

    3. C

      Every parent value is less than or equal to its children's values

    4. D

      Every node is larger than every node at the same level

    Correct answer
    A

    Every parent value is greater than or equal to its children's values

    Explanation

    In a max heap, each parent is no smaller than its children, placing a maximum value at the root.

  55. 55

    Why is a priority queue not technically the same thing as a heap?

    1. A

      A heap defines queue behavior but cannot store priorities

    2. B

      A priority queue cannot be implemented with a tree

    3. C

      A heap is an interface, while a priority queue is always an array

    4. D

      A priority queue is an abstract data type, while a heap is one possible implementation

    Correct answer
    D

    A priority queue is an abstract data type, while a heap is one possible implementation

    Explanation

    The priority queue specifies behavior; heaps are a common implementation, but other structures can also provide that behavior.

  56. 56

    Which condition would make a tree-shaped structure invalid as a heap even if its parent-child values appear correctly ordered?

    1. A

      It contains duplicate values

    2. B

      Its nodes are drawn at unusual horizontal positions

    3. C

      It contains a cycle and is therefore not a tree

    4. D

      Some leaf nodes have no children

    Correct answer
    C

    It contains a cycle and is therefore not a tree

    Explanation

    A heap must be a tree as well as satisfy the heap invariant. A cyclic structure is not a tree.

  57. 57

    Which is a use of priority queues described in the material?

    1. A

      Guaranteeing that every graph has a unique minimum spanning tree

    2. B

      Selecting the next promising node in algorithms such as Dijkstra's or best-first search

    3. C

      Storing only the visited nodes of a breadth-first search

    4. D

      Replacing the need for any graph representation

    Correct answer
    B

    Selecting the next promising node in algorithms such as Dijkstra's or best-first search

    Explanation

    Priority queues are used to choose the next node to explore in Dijkstra's algorithm and many best-first search algorithms.

  58. 58

    What are the typical complexities of peek, add, and poll in a binary-heap priority queue?

    1. A

      Peek: O(log⁡n)O(\log n)O(logn); add: O(n)O(n)O(n); poll: O(1)O(1)O(1)

    2. B

      Peek: O(n)O(n)O(n); add: O(1)O(1)O(1); poll: O(n)O(n)O(n)

    3. C

      All three operations: O(1)O(1)O(1)

    4. D

      Peek: O(1)O(1)O(1); add: O(log⁡n)O(\log n)O(logn); poll: O(log⁡n)O(\log n)O(logn)

    Correct answer
    D

    Peek: O(1)O(1)O(1); add: O(log⁡n)O(\log n)O(logn); poll: O(log⁡n)O(\log n)O(logn)

    Explanation

    The root can be inspected directly, while insertion and root removal may require a logarithmic-height swim or sink.

  59. 59

    What is the usual complexity of building a binary heap from an unordered array using heapify?

    1. A

      O(n2)O(n^2)O(n2)

    2. B

      O(log⁡n)O(\log n)O(logn)

    3. C

      O(n)O(n)O(n)

    4. D

      O(nlog⁡n)O(n\log n)O(nlogn)

    Correct answer
    C

    O(n)O(n)O(n)

    Explanation

    The bottom-up heapify procedure constructs a heap in linear time, unlike inserting the elements individually, which takes O(nlog⁡n)O(n\log n)O(nlogn).

  60. 60

    How can a min-priority queue be made to behave like a max-priority queue for numeric values?

    1. A

      Replace the heap with a FIFO queue

    2. B

      Negate values before insertion and negate them again when they are removed

    3. C

      Sort values in ascending order before every operation

    4. D

      Insert every value twice

    Correct answer
    B

    Negate values before insertion and negate them again when they are removed

    Explanation

    Negating numeric values reverses their order, so a min queue of negated values returns the original largest value first.

  61. 61

    What does the complete binary tree property require?

    1. A

      Every node has exactly two nonempty children

    2. B

      Every level except possibly the last is full, and the last level is filled from the left

    3. C

      Every level is filled from right to left

    4. D

      All leaves occur at exactly the same depth

    Correct answer
    B

    Every level except possibly the last is full, and the last level is filled from the left

    Explanation

    A complete binary tree fills all earlier levels and places the final level's nodes as far left as possible.

  62. 62

    For a zero-based array representation of a binary heap, where are the children of index iii?

    1. A

      At indices ⌊i/2⌋\lfloor i/2\rfloor⌊i/2⌋ and ⌊i/2⌋+1\lfloor i/2\rfloor+1⌊i/2⌋+1

    2. B

      At indices 2i+12i+12i+1 and 2i+22i+22i+2

    3. C

      At indices 2i2i2i and 2i+12i+12i+1

    4. D

      At indices i+1i+1i+1 and i+2i+2i+2

    Correct answer
    B

    At indices 2i+12i+12i+1 and 2i+22i+22i+2

    Explanation

    In a zero-based heap array, the left and right child indices are 2i+12i+12i+1 and 2i+22i+22i+2.

  63. 63

    Where is a newly inserted value placed in a complete binary heap before restoring the heap invariant?

    1. A

      At an arbitrary leaf chosen by the insertion value

    2. B

      At the leftmost leaf, even if that position is occupied

    3. C

      At the next open position at the bottom, as far left as possible

    4. D

      At the root, replacing the current root

    Correct answer
    C

    At the next open position at the bottom, as far left as possible

    Explanation

    The complete-tree property determines the insertion position: the next available bottom-level position, filled from the left.

  64. 64

    After inserting an element into a min heap, how is a heap-invariant violation typically repaired?

    1. A

      Remove the root and rebuild the heap

    2. B

      Sort all values in the heap

    3. C

      Swap the new value upward with larger parents until its position is valid

    4. D

      Swap the new value downward with smaller children

    Correct answer
    C

    Swap the new value upward with larger parents until its position is valid

    Explanation

    The inserted value may be smaller than its parent, so it swims or bubbles upward until the min-heap invariant holds.

  65. 65

    Why does removing the root of a heap usually involve moving the last array element to the root and sinking it?

    1. A

      The last element is always the next highest-priority value

    2. B

      Moving the last element automatically restores both heap properties

    3. C

      The root cannot be removed unless the whole array is sorted

    4. D

      The last element fills the root's position while preserving completeness, then must be repositioned to restore heap order

    Correct answer
    D

    The last element fills the root's position while preserving completeness, then must be repositioned to restore heap order

    Explanation

    Replacing the root with the final array element preserves the complete-tree shape; sinking may be needed to restore the heap invariant.

  66. 66

    Why does removing an arbitrary non-root element from a basic binary heap take linear time?

    1. A

      The heap generally must be scanned to find that element's position

    2. B

      The heap invariant forbids removal of non-root elements

    3. C

      The element's position is stored automatically in the root

    4. D

      Every non-root removal requires copying all nodes to a linked list

    Correct answer
    A

    The heap generally must be scanned to find that element's position

    Explanation

    Without an auxiliary index, finding an arbitrary value may require scanning all heap elements, taking O(n)O(n)O(n) time.

  67. 67

    How can a heap support faster lookup and removal of repeated values?

    1. A

      Sort the heap after each insertion and removal

    2. B

      Store each value only in the hash table and remove the heap

    3. C

      Assume every heap value is unique and store only one index

    4. D

      Maintain a hash table mapping each value to a set of heap indices and update it during swaps

    Correct answer
    D

    Maintain a hash table mapping each value to a set of heap indices and update it during swaps

    Explanation

    A value-to-indices map handles duplicates and avoids a linear scan; swaps must update the mapped indices.

  68. 68

    What is the main cost of adding an index map to a heap?

    1. A

      The heap can no longer support polling

    2. B

      The heap invariant stops applying to duplicate values

    3. C

      Additional linear space and overhead from maintaining the map during operations

    4. D

      Every lookup becomes slower than a linear scan

    Correct answer
    C

    Additional linear space and overhead from maintaining the map during operations

    Explanation

    The map can improve lookup and removal, but it uses extra space and requires additional work during swaps.

  69. 69

    What additional capability distinguishes an indexed priority queue from a traditional priority queue?

    1. A

      It removes the need for comparable values

    2. B

      It can efficiently update or delete a key-value pair identified by its key

    3. C

      It can store only values that are already sorted

    4. D

      It returns elements in their insertion order

    Correct answer
    B

    It can efficiently update or delete a key-value pair identified by its key

    Explanation

    An indexed priority queue tracks key identities and positions so particular entries can be updated or deleted efficiently.

  70. 70

    Why does an indexed priority queue maintain both a position map and an inverse map?

    1. A

      One maps key indices to heap positions; the other maps heap positions to key indices

    2. B

      They replace the need to store values in the queue

    3. C

      Both maps store the priority value, but in different formats

    4. D

      One identifies the root and the other identifies the last inserted value

    Correct answer
    A

    One maps key indices to heap positions; the other maps heap positions to key indices

    Explanation

    The position map supports locating a key in the heap, while the inverse map identifies which key occupies a heap position.

  71. 71

    In an indexed heap, why do swaps update the position and inverse maps rather than rearranging values by heap position?

    1. A

      Values are stored by key index, while the maps track where each key is positioned in the heap

    2. B

      Values are not stored in the indexed priority queue

    3. C

      The maps store the heap invariant directly

    4. D

      Heap positions cannot be changed after insertion

    Correct answer
    A

    Values are stored by key index, while the maps track where each key is positioned in the heap

    Explanation

    The values array is indexed by key index; swaps move keys between heap positions, so the maps must be updated to reflect the movement.

  72. 72

    Why can updating a specific key in an indexed binary heap take O(log⁡n)O(\log n)O(logn) time?

    1. A

      An update always sorts all key-value pairs

    2. B

      The key's position is found directly, then the changed value may swim or sink along the heap

    3. C

      The queue must scan all keys and rebuild the entire heap

    4. D

      The key's value is copied into every node

    Correct answer
    B

    The key's position is found directly, then the changed value may swim or sink along the heap

    Explanation

    The maps locate the key efficiently; restoring heap order may require moving the node along a tree of logarithmic height.

  73. 73

    What is the purpose of decrease-key and increase-key operations?

    1. A

      They remove the minimum and maximum values simultaneously

    2. B

      They restrict an update to lowering or raising a key's value, respectively

    3. C

      They change the number of children each heap node has

    4. D

      They change the key's identity without changing its value

    Correct answer
    B

    They restrict an update to lowering or raising a key's value, respectively

    Explanation

    These convenience operations update a value only if it becomes smaller or larger, as specified.

  74. 74

    What does a union-find data structure track?

    1. A

      The prefix sums of an array

    2. B

      The sorted order of elements in a binary search tree

    3. C

      The priority of every item in a queue

    4. D

      Elements partitioned into disjoint sets, supporting set lookup and set merging

    Correct answer
    D

    Elements partitioned into disjoint sets, supporting set lookup and set merging

    Explanation

    Union-find tracks disjoint groups and supports finding an element's group and merging groups.

  75. 75

    What does the find operation return in a union-find structure?

    1. A

      The next element to be merged

    2. B

      The shortest path between two elements

    3. C

      A representative identifying the set containing the given element

    4. D

      The size of the largest set

    Correct answer
    C

    A representative identifying the set containing the given element

    Explanation

    Find identifies which component or set contains an element, typically by returning its root representative.

  76. 76

    What does union do in a union-find structure?

    1. A

      Splits one set into two equally sized sets

    2. B

      Sorts every element within a set

    3. C

      Merges the sets containing two specified elements

    4. D

      Finds the shortest path between two vertices

    Correct answer
    C

    Merges the sets containing two specified elements

    Explanation

    Union combines the components containing its two input elements.

  77. 77

    How does path compression improve union-find operations?

    1. A

      It stores all elements in a sorted array

    2. B

      It prevents the union operation from changing component structure

    3. C

      It redirects nodes on a find path closer to the component root, shortening later lookups

    4. D

      It splits long paths into separate components

    Correct answer
    C

    It redirects nodes on a find path closer to the component root, shortening later lookups

    Explanation

    Path compression makes nodes encountered during find point directly or more closely to the root, greatly improving subsequent lookups.

  78. 78

    When union-by-size is used, which component's root is typically attached to the other?

    1. A

      The root with the smaller element value is deleted

    2. B

      The smaller component's root is attached to the larger component's root

    3. C

      The larger component's root is always attached to the smaller one

    4. D

      Both roots are replaced by new nodes

    Correct answer
    B

    The smaller component's root is attached to the larger component's root

    Explanation

    Merging the smaller tree into the larger one helps keep the resulting tree shallower.

  79. 79

    In a parent-array representation of union-find, what does a self-parenting entry indicate?

    1. A

      The element has been removed from the structure

    2. B

      The element has no associated index

    3. C

      The element is a root representative of its component

    4. D

      The element belongs to every component

    Correct answer
    C

    The element is a root representative of its component

    Explanation

    If an element's parent is itself, it is the root of its component.

  80. 80

    Why does Kruskal's minimum spanning tree algorithm use union-find?

    1. A

      To test whether an edge connects different components and would avoid creating a cycle

    2. B

      To select the largest-weight edge first

    3. C

      To ensure the graph has a unique minimum spanning tree

    4. D

      To sort each vertex's neighbors by distance

    Correct answer
    A

    To test whether an edge connects different components and would avoid creating a cycle

    Explanation

    Union-find checks whether an edge's endpoints are already connected; if so, adding the edge would create a cycle.

  81. 81

    Which sequence describes Kruskal's algorithm?

    1. A

      Sort vertices by degree and add every edge incident to the first vertex

    2. B

      Visit vertices breadth-first and add every edge encountered

    3. C

      Sort edges by weight, consider them in order, and add an edge only when its endpoints are in different components

    4. D

      Start with the heaviest edge and repeatedly remove the lightest cycle

    Correct answer
    C

    Sort edges by weight, consider them in order, and add an edge only when its endpoints are in different components

    Explanation

    Kruskal's algorithm processes edges in ascending weight order and uses component checks to avoid cycles.

  82. 82

    Which statement correctly defines a tree?

    1. A

      A directed graph in which every node has exactly two children

    2. B

      A connected, acyclic undirected graph

    3. C

      Any graph with at least one cycle

    4. D

      A graph in which every pair of vertices has multiple paths

    Correct answer
    B

    A connected, acyclic undirected graph

    Explanation

    A tree is a connected, acyclic undirected graph; equivalently, between any two vertices there is exactly one path.

  83. 83

    How many edges does a tree with nnn nodes have?

    1. A

      2n2n2n

    2. B

      n+1n+1n+1

    3. C

      n−1n-1n−1

    4. D

      nnn

    Correct answer
    C

    n−1n-1n−1

    Explanation

    A tree with nnn nodes has exactly n−1n-1n−1 edges.

  84. 84

    Which description defines a binary tree?

    1. A

      A tree whose values must be binary numbers

    2. B

      A tree in which each node has exactly two nonempty children

    3. C

      A tree in which each node has at most two children

    4. D

      A tree in which every node has at least two children

    Correct answer
    C

    A tree in which each node has at most two children

    Explanation

    A binary tree limits each node to at most two children; a node may have zero or one child.

  85. 85

    What additional property makes a binary tree a binary search tree?

    1. A

      Every node has exactly two children

    2. B

      Every level of the tree is completely filled

    3. C

      Each node's value is smaller than every value in the entire tree

    4. D

      Values in a node's left subtree are smaller and values in its right subtree are larger

    Correct answer
    D

    Values in a node's left subtree are smaller and values in its right subtree are larger

    Explanation

    A binary search tree maintains an ordering in which the left subtree contains smaller values and the right subtree contains larger values.

  86. 86

    Why must values inserted into a binary search tree be comparable?

    1. A

      Comparability guarantees the tree remains balanced

    2. B

      The tree stores values only as strings

    3. C

      Comparable values ensure the tree has no leaf nodes

    4. D

      The insertion procedure must decide whether each value belongs in the left or right subtree

    Correct answer
    D

    The insertion procedure must decide whether each value belongs in the left or right subtree

    Explanation

    Comparisons determine which branch to follow when inserting or searching for a value.

  87. 87

    What is the average-case search time in a reasonably shaped binary search tree, and what can happen in the worst case?

    1. A

      Average linear time; worst-case constant time

    2. B

      Average and worst-case quadratic time

    3. C

      Average constant time; worst-case logarithmic time

    4. D

      Average logarithmic time; worst-case linear time if the tree degenerates into a chain

    Correct answer
    D

    Average logarithmic time; worst-case linear time if the tree degenerates into a chain

    Explanation

    A reasonably balanced tree has logarithmic height, but an unbalanced tree can become a chain and require linear work.

  88. 88

    When inserting a value into a binary search tree, where does the search for its insertion position begin?

    1. A

      At the most recently inserted node

    2. B

      At an arbitrary node chosen from the tree

    3. C

      At the root, followed by comparisons down the tree

    4. D

      At the leftmost leaf, followed by a scan of all nodes

    Correct answer
    C

    At the root, followed by comparisons down the tree

    Explanation

    Insertion begins at the root and follows left or right branches according to comparisons until reaching an empty position.

  89. 89

    If duplicates are disallowed in a binary search tree, what should insertion do when the value already exists?

    1. A

      Leave the tree unchanged and report that no new value was inserted

    2. B

      Insert the duplicate at the root

    3. C

      Convert the tree into a heap

    4. D

      Replace every value in the left subtree

    Correct answer
    A

    Leave the tree unchanged and report that no new value was inserted

    Explanation

    An implementation that requires unique values can reject or ignore an insertion when that value is already present.

  90. 90

    When deleting a leaf node from a binary search tree, what is the simplest correct action?

    1. A

      Remove it and leave no replacement child

    2. B

      Replace it with the root node

    3. C

      Move it to the opposite subtree

    4. D

      Replace it with the largest value in the tree

    Correct answer
    A

    Remove it and leave no replacement child

    Explanation

    A leaf has no children, so it can be removed without needing a successor replacement.

  91. 91

    When deleting a node with exactly one child in a binary search tree, what should replace it?

    1. A

      Its sole child

    2. B

      The root of the entire tree

    3. C

      The smallest value in the entire tree

    4. D

      A new empty node

    Correct answer
    A

    Its sole child

    Explanation

    The node's only child takes its place, preserving the search-tree ordering.

  92. 92

    For a binary search tree node with two children, which value can serve as its successor replacement?

    1. A

      The largest value in its left subtree or the smallest value in its right subtree

    2. B

      The smallest value in its left subtree or the largest value in its right subtree

    3. C

      Only the root value of the entire tree

    4. D

      Any leaf value in the tree

    Correct answer
    A

    The largest value in its left subtree or the smallest value in its right subtree

    Explanation

    Either the predecessor from the left subtree or the successor from the right subtree preserves the binary search tree invariant.

  93. 93

    After copying a successor's value into a node being deleted, why must the successor node also be removed?

    1. A

      The root would lose its parent

    2. B

      The copied value would no longer be comparable

    3. C

      The left and right subtrees would become identical

    4. D

      Otherwise the successor's value would appear twice in the tree

    Correct answer
    D

    Otherwise the successor's value would appear twice in the tree

    Explanation

    Copying the successor value creates a duplicate until the original successor node is removed.

  94. 94

    What does an inorder traversal of a binary search tree produce?

    1. A

      The values in increasing order

    2. B

      The values in insertion order

    3. C

      The values in level-by-level order

    4. D

      The values in decreasing order

    Correct answer
    A

    The values in increasing order

    Explanation

    Inorder traversal visits the left subtree, the node, then the right subtree; the search-tree invariant makes this increasing order.

  95. 95

    How do preorder, inorder, and postorder traversals differ?

    1. A

      They differ only in whether they use a queue or a heap

    2. B

      They visit the right subtree first in every case

    3. C

      They print the current node before, between, or after the recursive subtree visits

    4. D

      They all print nodes in increasing order

    Correct answer
    C

    They print the current node before, between, or after the recursive subtree visits

    Explanation

    Preorder processes the node before its subtrees, inorder between them, and postorder after them.

  96. 96

    Which data structure is used to perform a level-order traversal?

    1. A

      A priority queue ordered by node depth

    2. B

      A stack

    3. C

      A queue

    4. D

      A hash set only

    Correct answer
    C

    A queue

    Explanation

    A queue processes nodes in discovery order, allowing a level-order traversal to visit nodes layer by layer.

  97. 97

    What does a tree rotation preserve when used in a binary search tree?

    1. A

      The order in which nodes were inserted

    2. B

      The binary search tree ordering, while changing the tree's shape

    3. C

      The exact depth of every node

    4. D

      The requirement that every node have two children

    Correct answer
    B

    The binary search tree ordering, while changing the tree's shape

    Explanation

    A correctly performed rotation changes the structure but preserves the relative ordering required by the binary search tree invariant.

  98. 98

    What is the purpose of balancing a binary search tree?

    1. A

      To make every node have exactly two children

    2. B

      To ensure each subtree has the same number of nodes

    3. C

      To keep its height logarithmic so operations avoid degrading to linear time

    4. D

      To store values in a contiguous array

    Correct answer
    C

    To keep its height logarithmic so operations avoid degrading to linear time

    Explanation

    A balanced tree limits its height, keeping search, insertion, and deletion logarithmic in the worst case.

  99. 99

    What is the AVL balance factor convention used in the material?

    1. A

      Left-subtree height minus right-subtree height

    2. B

      The total number of nodes in the right subtree

    3. C

      The difference between the root value and the node value

    4. D

      Right-subtree height minus left-subtree height

    Correct answer
    D

    Right-subtree height minus left-subtree height

    Explanation

    The described convention defines balance factor as right-subtree height minus left-subtree height.

  100. 100

    Which balance-factor values are allowed for each node in an AVL tree?

    1. A

      −1-1−1, 000, or 111

    2. B

      Only 000

    3. C

      −2-2−2, 000, or 222

    4. D

      Any integer, provided the root is balanced

    Correct answer
    A

    −1-1−1, 000, or 111

    Explanation

    An AVL tree requires every node's balance factor to be −1-1−1, 000, or 111; a value of −2-2−2 or 222 requires rebalancing.

  101. 101

    With the convention right-subtree height minus left-subtree height, what does a balance factor of −2-2−2 indicate?

    1. A

      The node is perfectly balanced

    2. B

      The node is left-heavy

    3. C

      The node is right-heavy

    4. D

      The node has no children

    Correct answer
    B

    The node is left-heavy

    Explanation

    A balance factor of −2-2−2 means the left subtree is taller than the right subtree by two.

  102. 102

    Which rotation repairs a left-left imbalance in an AVL tree?

    1. A

      A left rotation about the unbalanced node

    2. B

      A left rotation followed by a right rotation about the same node

    3. C

      No rotation; the imbalance is ignored

    4. D

      A right rotation about the unbalanced node

    Correct answer
    D

    A right rotation about the unbalanced node

    Explanation

    A left-left imbalance is repaired with a single right rotation about the unbalanced node.

  103. 103

    How is a left-right AVL imbalance repaired?

    1. A

      Rotate left at the left child, then rotate right at the unbalanced node

    2. B

      Rotate right at the left child, then rotate left at the unbalanced node

    3. C

      Rotate left twice at the unbalanced node

    4. D

      Rotate right once at the unbalanced node

    Correct answer
    A

    Rotate left at the left child, then rotate right at the unbalanced node

    Explanation

    The first left rotation converts the left-right case into a left-left case; a right rotation then resolves it.

  104. 104

    How is a right-left AVL imbalance repaired?

    1. A

      Rotate left at the right child, then rotate right at the unbalanced node

    2. B

      Rotate right once at the unbalanced node

    3. C

      Rotate left twice at the unbalanced node

    4. D

      Rotate right at the right child, then rotate left at the unbalanced node

    Correct answer
    D

    Rotate right at the right child, then rotate left at the unbalanced node

    Explanation

    A right rotation at the right child transforms the case into right-right; a left rotation then restores balance.

  105. 105

    Why must an AVL implementation update height and balance-factor information after a rotation?

    1. A

      The tree's ordering becomes invalid after every rotation

    2. B

      The rotation changes subtree relationships, so stored values may no longer be correct

    3. C

      Rotations change the values stored in every node

    4. D

      Heights are needed only when the tree is initially constructed

    Correct answer
    B

    The rotation changes subtree relationships, so stored values may no longer be correct

    Explanation

    Rotations change subtree shapes, so affected nodes' heights and balance factors must be recomputed, typically from lower nodes upward.

  106. 106

    What is a hash table designed to do?

    1. A

      Map unique keys to associated values using hashing

    2. B

      Guarantee that every key maps to a unique hash value

    3. C

      Represent a graph as a tree

    4. D

      Store only values in sorted order

    Correct answer
    A

    Map unique keys to associated values using hashing

    Explanation

    A hash table stores key-value associations and uses hashing to determine where keys are placed.

  107. 107

    What does a hash function do?

    1. A

      Sorts keys by their associated values

    2. B

      Identifies whether two objects are equal without comparing them

    3. C

      Maps a key to an integer in a fixed range

    4. D

      Guarantees a different result for every possible key

    Correct answer
    C

    Maps a key to an integer in a fixed range

    Explanation

    A hash function maps keys to integer values or table positions within a defined range.

  108. 108

    What is a hash collision?

    1. A

      A key is inserted into an empty table

    2. B

      A hash function returns a negative number

    3. C

      Two values associated with one key are equal

    4. D

      Two different keys produce the same hash value

    Correct answer
    D

    Two different keys produce the same hash value

    Explanation

    A collision occurs when distinct keys hash to the same value.

  109. 109

    If two keys have different hash values, what can be concluded?

    1. A

      The keys are not equal

    2. B

      The hash function is not deterministic

    3. C

      The keys must have the same value

    4. D

      The keys are guaranteed to be stored in adjacent slots

    Correct answer
    A

    The keys are not equal

    Explanation

    Equal objects must have compatible hash values; different hash values therefore establish that the objects are not equal.

  110. 110

    Why must a hash function be deterministic?

    1. A

      The table must remain sorted after every insertion

    2. B

      The hash function must return a random value each time

    3. C

      Every key must map to a different integer

    4. D

      The same key must map consistently so it can be found in the table

    Correct answer
    D

    The same key must map consistently so it can be found in the table

    Explanation

    If a key's hash changed between operations, the table might look in the wrong location and fail to find the key.

  111. 111

    Why are hash functions designed to distribute keys uniformly?

    1. A

      To ensure all keys have different values

    2. B

      To make hash computations depend on random numbers

    3. C

      To reduce collisions and avoid overloading a small number of table positions

    4. D

      To eliminate the need for collision resolution

    Correct answer
    C

    To reduce collisions and avoid overloading a small number of table positions

    Explanation

    A uniform distribution reduces clustering and collisions, supporting efficient table operations.

  112. 112

    Why do keys used in a hash table generally need to be immutable?

    1. A

      Changing a key automatically deletes every colliding entry

    2. B

      Immutable keys are the only values that can be compared

    3. C

      Hash tables cannot store integer keys

    4. D

      Changing a key can change its hash and make its stored entry difficult to locate

    Correct answer
    D

    Changing a key can change its hash and make its stored entry difficult to locate

    Explanation

    A key's hash should remain stable while it is stored; mutating the key could make its entry inaccessible.

  113. 113

    What is the purpose of a hash table's load factor?

    1. A

      It counts the number of distinct hash functions

    2. B

      It measures how full the table is relative to its capacity

    3. C

      It measures the average length of every key

    4. D

      It determines whether a key is immutable

    Correct answer
    B

    It measures how full the table is relative to its capacity

    Explanation

    The load factor is the number of stored items relative to the table's capacity and helps determine when resizing is needed.

  114. 114

    How can a hash table preserve efficient operations as it becomes too full?

    1. A

      Delete every key that shares a hash value

    2. B

      Convert all keys to array indices without checking them

    3. C

      Allocate a larger table and reinsert entries using their hash positions in the new table

    4. D

      Keep the same capacity and allow unlimited collisions

    Correct answer
    C

    Allocate a larger table and reinsert entries using their hash positions in the new table

    Explanation

    Resizing and rehashing entries reduces congestion and helps maintain efficient operations.

  115. 115

    How does separate chaining resolve a collision?

    1. A

      It stores colliding key-value entries together in a bucket data structure

    2. B

      It changes the hash function each time a collision occurs

    3. C

      It replaces the earlier key with the new key

    4. D

      It searches for an empty slot elsewhere in the array

    Correct answer
    A

    It stores colliding key-value entries together in a bucket data structure

    Explanation

    Separate chaining places entries with the same hash position in an auxiliary bucket, often a linked list.

  116. 116

    Which structures can be used as separate-chaining buckets?

    1. A

      Only queues with no search operation

    2. B

      Only static arrays

    3. C

      Only binary heaps

    4. D

      Linked lists, arrays, or trees

    Correct answer
    D

    Linked lists, arrays, or trees

    Explanation

    Buckets can use linked lists, arrays, binary trees, balanced trees, or other suitable structures.

  117. 117

    How does open addressing differ from separate chaining?

    1. A

      Open addressing stores entries only in linked lists, while chaining stores them only in one array

    2. B

      Open addressing probes for another slot in the table itself, while chaining stores collisions in bucket structures

    3. C

      Open addressing sorts entries, while chaining hashes values

    4. D

      Open addressing forbids collisions, while chaining changes the keys

    Correct answer
    B

    Open addressing probes for another slot in the table itself, while chaining stores collisions in bucket structures

    Explanation

    Open addressing keeps entries in the table and probes for available positions; separate chaining uses auxiliary bucket structures.

  118. 118

    What is the average-case time complexity of hash-table insertion, lookup, and removal with good hashing and collision handling?

    1. A

      O(n2)O(n^2)O(n2) for each operation

    2. B

      O(log⁡n)O(\log n)O(logn) for each operation

    3. C

      O(n)O(n)O(n) for each operation

    4. D

      O(1)O(1)O(1) for each operation

    Correct answer
    D

    O(1)O(1)O(1) for each operation

    Explanation

    With a well-distributed hash function and controlled load, these operations are constant time on average.

  119. 119

    What can happen to hash-table operation times if the hash function distributes keys poorly?

    1. A

      They become logarithmic regardless of the table's structure

    2. B

      They stop depending on the number of stored keys

    3. C

      They become guaranteed constant time

    4. D

      They can degrade to linear time because many keys collide

    Correct answer
    D

    They can degrade to linear time because many keys collide

    Explanation

    Many collisions can force operations to examine a long chain or probe through many occupied slots.

  120. 120

    In open addressing, what is a probing sequence used for?

    1. A

      Finding the value associated with a key without computing its hash

    2. B

      Determining the table's load factor

    3. C

      Sorting keys before they are hashed

    4. D

      Choosing successive table positions to try after a collision

    Correct answer
    D

    Choosing successive table positions to try after a collision

    Explanation

    A probing sequence determines which alternative positions to inspect when the original hash position is occupied.

  121. 121

    What can go wrong if an open-addressing probing sequence cycles through only some table slots?

    1. A

      All keys are automatically rehashed into separate chains

    2. B

      The hash function becomes nondeterministic

    3. C

      Every lookup returns the first key in the table

    4. D

      Insertion can loop indefinitely even though other table slots are empty

    Correct answer
    D

    Insertion can loop indefinitely even though other table slots are empty

    Explanation

    A short cycle may never reach an available position, so insertion can fail or loop even when the table has empty slots.

  122. 122

    For linear probing with step size aaa, what condition ensures the probe can cycle through every slot in a table of size nnn?

    1. A

      aaa and nnn are both even

    2. B

      aaa is a multiple of nnn

    3. C

      aaa and nnn are relatively prime

    4. D

      aaa equals nnn

    Correct answer
    C

    aaa and nnn are relatively prime

    Explanation

    The step and table size must have greatest common divisor one so the linear probe visits every position before repeating.

  123. 123

    What is quadratic probing?

    1. A

      A method that probes only neighboring positions

    2. B

      A method that stores each collision in a binary tree

    3. C

      A collision-resolution method that offsets positions according to a quadratic function of the probe number

    4. D

      A hash function that squares the key and never produces collisions

    Correct answer
    C

    A collision-resolution method that offsets positions according to a quadratic function of the probe number

    Explanation

    Quadratic probing uses offsets based on a quadratic expression, such as a function involving x2x^2x2.

  124. 124

    Why must quadratic probing use carefully chosen parameters and table sizes?

    1. A

      Quadratic expressions cannot be evaluated for integer inputs

    2. B

      Quadratic probing works only when keys are strings

    3. C

      Some choices produce short cycles that fail to visit every table position

    4. D

      The table must contain no empty positions

    Correct answer
    C

    Some choices produce short cycles that fail to visit every table position

    Explanation

    Many quadratic probing functions do not visit all slots for arbitrary table sizes, so specific combinations are needed to avoid cycles.

  125. 125

    What makes double hashing different from ordinary linear probing?

    1. A

      Its step size is derived from a second hash function applied to the key

    2. B

      It uses two separate hash tables for every key

    3. C

      It changes the key after each collision

    4. D

      It stores each key in two different buckets

    Correct answer
    A

    Its step size is derived from a second hash function applied to the key

    Explanation

    Double hashing uses a secondary hash function to determine the probe step for each key.

  126. 126

    Why does open-addressing deletion commonly use a tombstone rather than replacing the deleted slot with empty?

    1. A

      An empty slot would prevent new values from being inserted

    2. B

      A tombstone stores the deleted key's new value

    3. C

      A tombstone lets later searches continue through the probe sequence

    4. D

      A tombstone keeps the table permanently at the same load factor

    Correct answer
    C

    A tombstone lets later searches continue through the probe sequence

    Explanation

    Marking the slot empty could make a search stop before reaching a key placed later in the same probe sequence; a tombstone signals that probing should continue.

  127. 127

    How can an insertion reuse a tombstone encountered during its probe sequence?

    1. A

      Convert every tombstone in the table into an empty slot

    2. B

      Stop immediately and overwrite the tombstone without checking for an existing key

    3. C

      Remember the first tombstone, continue searching for an existing key, then insert at the tombstone if appropriate

    4. D

      Move the tombstone to the root of the table

    Correct answer
    C

    Remember the first tombstone, continue searching for an existing key, then insert at the tombstone if appropriate

    Explanation

    Continuing the search avoids creating a duplicate key; if no existing key is found, the remembered tombstone can be reused.

  128. 128

    What happens to tombstones when an open-addressing table is resized and its entries are reinserted?

    1. A

      They must be copied into the same numerical positions

    2. B

      They can be discarded because only live key-value entries are reinserted

    3. C

      They become keys in the new table

    4. D

      They prevent the new table from accepting insertions

    Correct answer
    B

    They can be discarded because only live key-value entries are reinserted

    Explanation

    Reinserting only live entries into the new table removes the old tombstones.

  129. 129

    What does a Fenwick tree support efficiently?

    1. A

      Range-sum queries and point updates on an array

    2. B

      Priority-based removal and key updates

    3. C

      Finding connected components in a graph

    4. D

      Arbitrary substring search and suffix sorting

    Correct answer
    A

    Range-sum queries and point updates on an array

    Explanation

    A Fenwick tree supports efficient prefix or range sums and updates to individual array positions.

  130. 130

    Why are ordinary prefix sums inconvenient when array values are frequently updated?

    1. A

      Changing one value can require recomputing many later prefix sums

    2. B

      Prefix sums cannot answer range-sum queries

    3. C

      They require every array value to be negative

    4. D

      They take linear time to read any single stored prefix sum

    Correct answer
    A

    Changing one value can require recomputing many later prefix sums

    Explanation

    A point change affects every later prefix sum, so maintaining all prefix sums can require linear work.

  131. 131

    What range of indices is conventionally used by a Fenwick tree in the material?

    1. A

      Zero-based indices only

    2. B

      One-based indices

    3. C

      Indices beginning at the array's midpoint

    4. D

      Negative indices

    Correct answer
    B

    One-based indices

    Explanation

    Fenwick-tree operations are described using one-based indices, which are important for the least-significant-bit calculations.

  132. 132

    What determines the range of entries represented by a Fenwick-tree cell?

    1. A

      The number of children in the original array

    2. B

      The value stored in the cell

    3. C

      The index's most significant set bit only

    4. D

      The value of the index's least significant set bit

    Correct answer
    D

    The value of the index's least significant set bit

    Explanation

    A cell covers a range whose size is determined by the least significant set bit of its index.

  133. 133

    How is a Fenwick-tree prefix sum typically computed?

    1. A

      Start at the requested index and repeatedly subtract its least significant set bit until reaching zero

    2. B

      Follow child pointers from a root node

    3. C

      Start at the last index and repeatedly double the index

    4. D

      Start at zero and scan every array position up to the requested index

    Correct answer
    A

    Start at the requested index and repeatedly subtract its least significant set bit until reaching zero

    Explanation

    The prefix-sum procedure accumulates the relevant cells while cascading downward by subtracting the least significant set bit.

  134. 134

    How can a Fenwick tree obtain the sum over indices from iii through jjj using prefix sums?

    1. A

      Add the prefix sum through iii to the prefix sum through jjj

    2. B

      Multiply the two prefix sums

    3. C

      Subtract the prefix sum through i−1i-1i−1 from the prefix sum through jjj

    4. D

      Subtract the prefix sum through jjj from the prefix sum through iii

    Correct answer
    C

    Subtract the prefix sum through i−1i-1i−1 from the prefix sum through jjj

    Explanation

    The range sum is the cumulative sum through jjj minus the cumulative sum before the range, through i−1i-1i−1.

  135. 135

    During a Fenwick-tree point update at index iii, which positions must be updated?

    1. A

      Every index smaller than iii

    2. B

      The cells responsible for iii, reached by repeatedly adding the least significant set bit

    3. C

      Every index whose value equals the updated value

    4. D

      Only the cell at index iii

    Correct answer
    B

    The cells responsible for iii, reached by repeatedly adding the least significant set bit

    Explanation

    A point update changes the target cell and its responsible ancestors by repeatedly adding the least significant set bit to the index.

  136. 136

    What is the time complexity of constructing a Fenwick tree using the described linear-time propagation method?

    1. A

      O(nlog⁡n)O(n\log n)O(nlogn)

    2. B

      O(n)O(n)O(n)

    3. C

      O(log⁡n)O(\log n)O(logn)

    4. D

      O(n2)O(n^2)O(n2)

    Correct answer
    B

    O(n)O(n)O(n)

    Explanation

    The linear construction propagates each value to its responsible parent in a single pass, taking O(n)O(n)O(n) time.

  137. 137

    What is a suffix of a string in this material?

    1. A

      A prefix that ends before the middle of the string

    2. B

      A sequence of characters sorted in reverse order

    3. C

      Any substring that begins at the first character

    4. D

      A nonempty substring that reaches the end of the string

    Correct answer
    D

    A nonempty substring that reaches the end of the string

    Explanation

    A suffix is a nonempty substring ending at the final character of the original string.

  138. 138

    What does a suffix array store?

    1. A

      Every suffix as a separate copied string in insertion order

    2. B

      The number of repeated characters in each suffix

    3. C

      The starting indices of a string's suffixes in lexicographic order

    4. D

      The ending indices of all prefixes in numerical order

    Correct answer
    C

    The starting indices of a string's suffixes in lexicographic order

    Explanation

    A suffix array stores indices ordered according to the lexicographic order of the suffixes starting at those positions.

  139. 139

    Why can a suffix array store indices rather than storing every suffix explicitly?

    1. A

      Suffixes do not share characters with the original string

    2. B

      An index contains all characters of its suffix

    3. C

      Each index identifies a suffix within the original string, providing a compact representation

    4. D

      Indices automatically sort themselves without comparisons

    Correct answer
    C

    Each index identifies a suffix within the original string, providing a compact representation

    Explanation

    The original string and each suffix's starting index are enough to represent the suffix, avoiding copies of all suffix strings.

  140. 140

    What does an LCP-array entry represent?

    1. A

      The number of distinct characters in the original string

    2. B

      The length of the longest suffix in the string

    3. C

      The number of initial characters shared by two adjacent sorted suffixes

    4. D

      The number of suffixes beginning with a particular character

    Correct answer
    C

    The number of initial characters shared by two adjacent sorted suffixes

    Explanation

    Each LCP entry records the length of the longest common prefix shared by neighboring suffixes in suffix-array order.

  141. 141

    Why is the first LCP-array entry conventionally set to zero?

    1. A

      The first suffix always has length zero

    2. B

      The first two suffixes are always identical

    3. C

      A zero marks the end of the original string

    4. D

      It has no preceding suffix in the sorted array to compare against

    Correct answer
    D

    It has no preceding suffix in the sorted array to compare against

    Explanation

    The first suffix has no previous suffix with which to form an adjacent pair, so its LCP value is undefined and conventionally stored as zero.

  142. 142

    How can a suffix array and LCP array help count distinct substrings?

    1. A

      Subtract the number of suffixes from n!n!n!

    2. B

      Count the suffixes and multiply by the maximum LCP value

    3. C

      Start with n(n+1)2\frac{n(n+1)}{2}2n(n+1)​ substrings and subtract the sum of the LCP values

    4. D

      Add all LCP values to the number of characters

    Correct answer
    C

    Start with n(n+1)2\frac{n(n+1)}{2}2n(n+1)​ substrings and subtract the sum of the LCP values

    Explanation

    There are n(n+1)2\frac{n(n+1)}{2}2n(n+1)​ total substrings, and the LCP values account for repeated substrings that should not be counted again.

  143. 143

    How can the longest repeated substring be found from an LCP array?

    1. A

      Find the minimum LCP value and read the entire string

    2. B

      Find the maximum LCP value and read that many characters from its corresponding suffix

    3. C

      Sort the LCP values and use their sum as the substring length

    4. D

      Choose the suffix with the greatest starting index

    Correct answer
    B

    Find the maximum LCP value and read that many characters from its corresponding suffix

    Explanation

    The largest LCP value gives the longest shared prefix between two distinct suffixes, which is a repeated substring.

  144. 144

    How can the longest common substring shared by at least kkk of several strings be approached with suffix and LCP arrays?

    1. A

      Find the maximum single character frequency in each string

    2. B

      Sort the original strings by length and compare only the shortest and longest

    3. C

      Concatenate the strings with distinct sentinels, then find a window containing enough source-string colors with a large minimum LCP

    4. D

      Concatenate the strings without separators and select the longest suffix

    Correct answer
    C

    Concatenate the strings with distinct sentinels, then find a window containing enough source-string colors with a large minimum LCP

    Explanation

    Distinct sentinels separate the strings; a window of suffixes from enough different source strings can be evaluated using its minimum LCP.

  145. 145

    Why should distinct sentinel symbols be placed between concatenated strings for the suffix-array method?

    1. A

      They guarantee that all suffixes have the same length

    2. B

      They remove the need to construct an LCP array

    3. C

      They make the original strings identical

    4. D

      They prevent suffixes from improperly continuing across string boundaries

    Correct answer
    D

    They prevent suffixes from improperly continuing across string boundaries

    Explanation

    Distinct sentinels prevent a suffix from treating the end of one source string and the beginning of another as a continuous substring.

  146. 146

    Why should sentinel values sort before the ordinary characters in the concatenated text?

    1. A

      They ensure the longest common substring contains a sentinel

    2. B

      They make all ordinary suffixes equal

    3. C

      They allow the suffix array to be stored without indices

    4. D

      Their suffixes can be placed before the ordinary suffixes and then ignored in the relevant comparison

    Correct answer
    D

    Their suffixes can be placed before the ordinary suffixes and then ignored in the relevant comparison

    Explanation

    Sentinels smaller than ordinary characters sort their suffixes separately at the beginning, where they can be excluded from the substantive search.

  147. 147

    In the sliding-window method for a kkk-string longest-common-substring problem, when is a window valid?

    1. A

      When it contains suffixes from at least kkk different source strings

    2. B

      When every suffix in it comes from the same source string

    3. C

      When its first LCP value is the largest in the entire array

    4. D

      When it contains exactly kkk suffixes total

    Correct answer
    A

    When it contains suffixes from at least kkk different source strings

    Explanation

    The window must represent at least kkk distinct source strings so that its common prefix is shared among them.

  148. 148

    How is the common-prefix length for a window of adjacent suffixes determined?

    1. A

      By the number of distinct characters in the concatenated text

    2. B

      By the minimum relevant LCP value within the window

    3. C

      By the maximum suffix length in the window

    4. D

      By the sum of all suffix-array indices in the window

    Correct answer
    B

    By the minimum relevant LCP value within the window

    Explanation

    Every suffix in the window shares only as much prefix as the weakest adjacent LCP link, so the minimum LCP determines the common length.

  149. 149

    In the described sliding-window search, what should the algorithm do when the window contains fewer than kkk source-string colors?

    1. A

      Shrink the window further

    2. B

      Stop, because no valid answer exists

    3. C

      Expand the window to include more suffixes

    4. D

      Replace the LCP array with the suffix array

    Correct answer
    C

    Expand the window to include more suffixes

    Explanation

    The window must gain another source-string color before it can form a valid candidate, so it is expanded.

  150. 150

    What is the total number of nonempty substrings in a string of length nnn?

    1. A

      n!n!n!

    2. B

      n2n^2n2

    3. C

      2n2^n2n

    4. D

      n(n+1)2\frac{n(n+1)}{2}2n(n+1)​

    Correct answer
    D

    n(n+1)2\frac{n(n+1)}{2}2n(n+1)​

    Explanation

    There are nnn substrings of length one, n−1n-1n−1 of length two, and so on, totaling n(n+1)2\frac{n(n+1)}{2}2n(n+1)​.

Question 1 of 150
Question 1 of 150

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent