1. Home
  2. Explore
  3. Computer Science
  4. Data Structures and Algorithms

Study methods

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

Data Structures and Algorithms

  • 82Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Search Foundations

What is an algorithm?

82 questions

  1. 1

    What is an algorithm?

    Model answer

    An algorithm is a sequence of step-by-step instructions for solving a problem.

    Explanation

    Algorithms describe the logical procedure used to produce an answer.

  2. 2

    Why can the way data is stored affect an algorithm's performance?

    Model answer

    The organization and representation of data determine how quickly an algorithm can access and manipulate it.

    Explanation

    A correct algorithm may still be inefficient if its data structure requires slow access or excessive space.

  3. 3

    Why could a search of randomly arranged numbers require approximately nnn steps?

    Model answer

    Without information about the values or their order, the search may need to inspect almost every one of the nnn elements.

    Explanation

    Random placement provides no basis for eliminating unexamined elements.

  4. 4

    Why is guessing the location of a value not a reliable search algorithm?

    Model answer

    A guess may succeed quickly by chance, but it cannot guarantee the same efficiency on repeated searches.

    Explanation

    For seven randomly placed values, directly guessing the target has only a one-in-seven chance of succeeding immediately.

  5. 5

    What information makes binary search possible?

    Model answer

    The data must be sorted so that each comparison allows half of the remaining search space to be eliminated.

    Explanation

    Sorting supplies the ordering needed to decide whether to continue to the left or right.

  6. 6

    Describe the basic process of binary search.

    Model answer

    Check the middle element, determine whether the target is smaller or larger, and repeat the process in the appropriate half.

    Explanation

    Each repetition divides the remaining problem approximately in half.

  7. 7

    Why does binary search require data to be stored back-to-back in an array?

    Model answer

    Contiguous storage lets the computer calculate the address of the middle element directly from its index.

    Explanation

    If elements were scattered unpredictably, arithmetic on an index would not identify the correct memory location.

  8. 8

    What is the difference between a human moving through a sequence and a computer accessing an array element?

    Model answer

    A human may need to move through intervening positions, whereas a computer can calculate an array address and access it in constant time.

    Explanation

    Random access is one reason computers can search and manipulate organized data efficiently.

  9. 9

    How does zero-based indexing identify the middle of seven elements?

    Model answer

    The positions are 000 through 666, and the middle position is found by computing rac{7}{2} and rounding down to 333.

    Explanation

    The middle element is therefore at index 333.

  10. 10

    What does it mean for an array to be contiguous?

    Model answer

    Its values occupy consecutive memory locations with no gaps between them.

    Explanation

    Contiguous placement supports arithmetic address calculation and random access.

  11. 11

    What is the worst-case running time of linear search?

    Model answer

    Its worst-case running time is O(n)O(n)O(n) because it may inspect all nnn elements.

    Explanation

    The target may be at the final position or may be absent.

  12. 12

    What is the running time of binary search in terms of the number of elements?

    Model answer

    Binary search takes logarithmic time, typically O(log⁡2n)O(\log_2 n)O(log2​n).

    Explanation

    The search space is repeatedly divided by two.

  13. 13

    Why is logarithmic growth more efficient than linear growth for large inputs?

    Model answer

    A logarithmic running time increases much more slowly as nnn becomes large than a linear running time.

    Explanation

    Repeatedly halving a large problem requires relatively few additional steps.

  14. 14

    What does Big O notation describe?

    Model answer

    Big O gives an upper bound on an algorithm's running time as the input size grows.

    Explanation

    For example, linear search has an upper bound of O(n)O(n)O(n).

  15. 15

    What does capital omega notation describe?

    Model answer

    Omega gives a lower bound on an algorithm's running time.

    Explanation

    For linear search, the best case can be expressed as a9(1) when the target is first.

  16. 16

    What does capital theta notation mean?

    Model answer

    Theta indicates that an algorithm has matching upper and lower bounds of the same asymptotic form.

    Explanation

    If both bounds are described by the same formula, the running time is b8 of that formula.

  17. 17

    Why can linear search have a best-case time of a9(1)?

    Model answer

    The target may happen to be the first element, so the search finishes after a constant number of steps.

    Explanation

    This best-case time does not depend on the total number of elements.

  18. 18

    What is the relationship between sorting and binary search?

    Model answer

    Sorting requires upfront work, but it enables later searches to use binary search in logarithmic rather than linear time.

    Explanation

    The cost of organizing the data can provide faster repeated searches.

  19. 19

    Why is sorting not as fast as binary searching the same data?

    Model answer

    Searching can discard half the data at each step, but sorting must process all elements so that none remain unordered.

    Explanation

    Divide-and-conquer sorting improves efficiency but cannot simply ignore half of the elements.

  20. 20

    Compare O(n)O(n)O(n), O(log n), and O(1)O(1)O(1) in terms of growth.

    Model answer

    O(1)O(1)O(1) remains constant, O(log n) grows slowly, and O(n)O(n)O(n) grows proportionally with the input size.

    Explanation

    These notations describe increasingly greater growth as nnn becomes large.

  21. 21

    What is bubble sort?

    Model answer

    Bubble sort repeatedly compares adjacent elements and swaps them when they are out of order.

    Explanation

    Repeated adjacent swaps move larger elements toward one end of the sequence.

  22. 22

    Why is bubble sort called bubble sort?

    Model answer

    Large values appear to bubble toward one end of the sequence as adjacent swaps move them repeatedly.

    Explanation

    Equivalently, smaller values can move toward the opposite end.

  23. 23

    State the pseudocode idea behind bubble sort.

    Model answer

    Repeat until no swaps occur: for each index iii from 000 to n−2n-2n−2, swap elements iii and i+1i+1i+1 if they are out of order.

    Explanation

    The loop stops after a complete pass makes no changes.

  24. 24

    Why does bubble sort compare indices only through n−2n-2n−2?

    Model answer

    Each element at index iii is compared with the element at index i+1i+1i+1, so iii must stop one position before the final index n−1n-1n−1.

    Explanation

    This prevents the comparison from referring to an index beyond the array.

  25. 25

    How does bubble sort turn local improvements into a globally sorted sequence?

    Model answer

    Each adjacent swap fixes a local inversion, and repeated passes combine these local corrections until the entire sequence is ordered.

    Explanation

    No single swap necessarily solves the whole problem, but the accumulated swaps do.

  26. 26

    What is selection sort?

    Model answer

    Selection sort repeatedly finds the smallest remaining element and swaps it into the next correct position.

    Explanation

    After each iteration, one additional position at the beginning is finalized.

  27. 27

    State the pseudocode idea behind selection sort.

    Model answer

    For each index iii from 000 to n−1n-1n−1, find the smallest element from positions iii through n−1n-1n−1 and swap it with the element at iii.

    Explanation

    The unsorted portion becomes smaller after every selection.

  28. 28

    Why does selection sort do less work on each successive iteration?

    Model answer

    The elements already placed at the beginning are excluded, so the remaining search covers nnn, then n−1n-1n−1, then n−2n-2n−2, and so on.

    Explanation

    Each iteration permanently settles one position.

  29. 29

    Why are bubble sort and selection sort generally classified as quadratic algorithms?

    Model answer

    Both perform a number of comparisons proportional to roughly nnn for roughly nnn stages, producing O(n2)O(n^2)O(n2) growth.

    Explanation

    Repeatedly scanning or rechecking the sequence creates the quadratic term.

  30. 30

    What summation describes the comparisons in selection sort?

    Model answer

    The comparisons sum to n-1+n-2+dots+1= rac{n(n-1)}{2}, which is O(n2)O(n^2)O(n2).

    Explanation

    Expanding the expression gives rac{n^2}{2}- rac{n}{2}, whose dominant term is quadratic.

  31. 31

    Why can lower-order terms be ignored in asymptotic analysis?

    Model answer

    As nnn becomes large, the highest-order term dominates the growth of the running time.

    Explanation

    For example, rac{n^2}{2}- rac{n}{2} is classified as O(n2)O(n^2)O(n2).

  32. 32

    What is merge sort?

    Model answer

    Merge sort recursively divides the data into halves, sorts the smaller parts, and merges the sorted halves.

    Explanation

    A one-element sequence is already sorted, and larger sequences are assembled from sorted subproblems.

  33. 33

    How does merge sort use divide and conquer?

    Model answer

    It repeatedly divides the sequence into smaller halves, solves each half, and combines the results by interweaving them in order.

    Explanation

    The method applies the same strategy recursively until individual elements remain.

  34. 34

    What is the principal time-space trade-off between merge sort and bubble or selection sort?

    Model answer

    Merge sort is faster, typically O(nlog⁡n)O(n\log n)O(nlogn), but uses additional temporary space, whereas bubble and selection sort use less extra space but take O(n2)O(n^2)O(n2) time.

    Explanation

    Merge sort gains speed by temporarily storing portions of the data while merging them.

  35. 35

    Why is a single element considered already sorted in merge sort?

    Model answer

    A sequence containing only one element has no pair of elements that can be out of order.

    Explanation

    This is the base case that ends the recursive division.

  36. 36

    What does RAM allow a computer to do in constant time?

    Model answer

    RAM allows the computer to access a particular byte by its address without moving through the preceding bytes.

    Explanation

    The term random access refers to direct access by address, not to random placement.

  37. 37

    How are memory addresses related to array indices?

    Model answer

    An array index identifies the position of an element, and arithmetic on that index can be used to calculate its memory address.

    Explanation

    This works because array elements occupy predictable, consecutive locations.

  38. 38

    What is an array?

    Model answer

    An array is a contiguous block of memory in which values are stored consecutively.

    Explanation

    Its elements can be accessed efficiently using their indices.

  39. 39

    Why are bits commonly grouped into bytes?

    Model answer

    Computers generally treat memory in units such as bytes, with each byte containing 888 bits.

    Explanation

    Values such as numbers are represented using patterns of these bits and bytes.

  40. 40

    What are the first and last indices of an array containing six values under zero-based indexing?

    Model answer

    The first index is 000 and the last index is 555.

    Explanation

    An array of length nnn has indices from 000 through n−1n-1n−1.

  41. 41

    Why can an array provide constant-time random access?

    Model answer

    Because the address of any element can be calculated directly from its index and the element size.

    Explanation

    No traversal through earlier elements is required.

  42. 42

    What limitation arises when an array needs to grow beyond its allocated space?

    Model answer

    If adjacent memory is occupied, the array may need to be relocated to a larger contiguous region.

    Explanation

    Moving all existing values to the new location takes additional time.

  43. 43

    Why is preallocating a very large array wasteful?

    Model answer

    Unused reserved memory cannot be used by other parts of the program or other programs.

    Explanation

    The programmer trades possible future growth for current space consumption.

  44. 44

    What factors influence the choice of a data structure?

    Model answer

    The choice depends on trade-offs among time, memory space, hardware cost, development effort, and the required operations.

    Explanation

    No single structure is optimal for every problem.

  45. 45

    Why does a floating-point value have limited precision when stored in a fixed number of bits?

    Model answer

    Only a fixed number of bits or bytes is available to represent its value, limiting how many digits can be stored.

    Explanation

    For example, allocating 323232 or 646464 bits imposes a finite representation range and precision.

  46. 46

    What is a linked list?

    Model answer

    A linked list is a dynamic data structure made of nodes connected by stored addresses or pointers to the next nodes.

    Explanation

    The nodes may occupy separate, noncontiguous locations in memory.

  47. 47

    What information does a linked-list node typically contain?

    Model answer

    A node contains the data value and a pointer or address identifying the next node.

    Explanation

    The pointer implements the conceptual arrow connecting nodes.

  48. 48

    How can a linked list grow without requiring one contiguous memory block?

    Model answer

    A new node can be placed wherever memory is available and linked to the existing structure by updating a pointer.

    Explanation

    The nodes do not need to be physically adjacent.

  49. 49

    How can an element be removed from a linked list?

    Model answer

    The surrounding pointer is changed to skip the node being removed and point to the following node.

    Explanation

    Removing a node usually changes links rather than shifting all later values.

  50. 50

    Why can a linked list not use binary search directly?

    Model answer

    Its nodes are not guaranteed to be contiguous, so an index calculation cannot directly locate the middle node.

    Explanation

    The list must be followed from its first node to reach later nodes.

  51. 51

    What is the worst-case access time for an element in a linked list?

    Model answer

    Access can take linear time, O(n)O(n)O(n), because the list may need to be traversed from the first node.

    Explanation

    Only the beginning of the list is directly known.

  52. 52

    What space overhead does a linked list introduce compared with an array?

    Model answer

    Each node needs extra space for a pointer in addition to the stored value.

    Explanation

    The pointer records the address of the next node.

  53. 53

    What is the main advantage of a linked list over an array?

    Model answer

    A linked list can grow and shrink dynamically without requiring a predetermined contiguous block of memory.

    Explanation

    This flexibility comes at the cost of extra pointer storage and slower access.

  54. 54

    What is a pointer in the context of linked structures?

    Model answer

    A pointer is a stored address or index that identifies the location of another node.

    Explanation

    Pointers allow a program to represent links using binary data in memory.

  55. 55

    How is the end of a linked list represented?

    Model answer

    The final node stores a special null-like value, often represented by all zero bits, to indicate that no next node exists.

    Explanation

    This value marks the termination of traversal.

  56. 56

    What is a binary search tree?

    Model answer

    A binary search tree is a tree in which each node has at most two children, with smaller values in the left subtree and larger values in the right subtree.

    Explanation

    The ordering rule applies recursively at every node.

  57. 57

    What are the root, left child, and right child of a tree?

    Model answer

    The root is the starting node; a node's left child is connected on its left, and its right child is connected on its right.

    Explanation

    Nodes below a node form its subtrees.

  58. 58

    How does a binary search tree support dynamic insertion?

    Model answer

    A new value is compared with nodes from the root and placed in an available left or right position according to the ordering rule.

    Explanation

    For example, a value greater than both 161616 and 424242 can be placed to the right of 424242.

  59. 59

    How would you search a binary search tree for 151515 if the root is 161616 and its left child is 888?

    Model answer

    Move left from 161616 because 15<1615<1615<16, then move right from 888 because 15>815>815>8.

    Explanation

    The ordering property directs each comparison toward the possible location.

  60. 60

    Why can a balanced binary search tree provide logarithmic search time?

    Model answer

    Each comparison selects one of two subtrees, reducing the remaining height of a balanced tree roughly by half at each step.

    Explanation

    Search and insertion can ideally take O(log_2 n) time.

  61. 61

    What can happen if values are inserted into a binary search tree in a highly ordered sequence?

    Model answer

    The tree can become long and one-sided, effectively degenerating into a linked list.

    Explanation

    In that case, operations may become linear rather than logarithmic.

  62. 62

    Why might a binary search tree need rebalancing?

    Model answer

    Rebalancing prevents the tree from becoming excessively tall and preserves efficient search and insertion.

    Explanation

    Some tree structures automatically reorganize their nodes to maintain a small height.

  63. 63

    What is the space cost of a binary tree compared with a linked list?

    Model answer

    A node may require up to two child pointers instead of the linked list's one next pointer.

    Explanation

    The additional pointers use more space but support a two-dimensional organization.

  64. 64

    What is a hash table?

    Model answer

    A hash table is a data structure that uses a hash function to map keys to array positions for fast access.

    Explanation

    With a good hash function and few collisions, lookup and insertion can be approximately constant time.

  65. 65

    How could a hash table place names into buckets using their first letters?

    Model answer

    It could convert the first letter to a numeric code and use that code, adjusted for zero-based indexing, as the bucket index.

    Explanation

    For example, ASCII code 656565 for capital A can be converted to index 000 by subtracting 656565.

  66. 66

    Why does a hash table provide fast insertion when its hash function is suitable?

    Model answer

    The hash function computes the target bucket directly, so the table does not need to search all existing entries.

    Explanation

    The number of hashing steps remains constant with respect to the total number of stored items.

  67. 67

    What is a collision in a hash table?

    Model answer

    A collision occurs when two or more different keys are mapped to the same bucket.

    Explanation

    Names such as David and Doug collide if the hash function uses only their shared first letter.

  68. 68

    How can a hash table handle collisions?

    Model answer

    It can store colliding entries in a linked structure associated with the same bucket.

    Explanation

    The array provides fast access to the bucket, while the linked list stores multiple matching keys.

  69. 69

    Why can a poor hash function make a hash table operate in linear time?

    Model answer

    If many keys map to one bucket, the bucket's linked list becomes long and must be searched sequentially.

    Explanation

    In the worst case, the entire table effectively behaves like one linked list.

  70. 70

    How could using the first two letters instead of the first letter improve a name hash function?

    Model answer

    It creates more buckets and usually distributes names more evenly, reducing collisions.

    Explanation

    Names beginning with different two-letter combinations are less likely to share a bucket.

  71. 71

    Why is hash-table performance described as approximately or ideally constant time rather than always constant time?

    Model answer

    Access is constant when collisions are minimal, but collisions can require searching a linked list.

    Explanation

    The actual performance depends on the quality of the hash function and the distribution of keys.

  72. 72

    What is a key-value relationship in a hash table?

    Model answer

    A key is the input used to locate an associated value, and the value is the data stored for that key.

    Explanation

    A dictionary can use a name as a key and associate it with information such as a Boolean status.

  73. 73

    How does a Python dictionary relate to a hash table?

    Model answer

    A Python dictionary is a high-level implementation of a hash table that associates keys with values.

    Explanation

    The language hides the lower-level hashing, indexing, and collision-handling details.

  74. 74

    How does a Python list relate to an array and a linked structure?

    Model answer

    A Python list provides array-like indexed storage while supporting dynamic growth and shrinking through implementation mechanisms hidden by the language.

    Explanation

    It combines a familiar sequence interface with more flexibility than a fixed-size array.

  75. 75

    What is a Boolean value?

    Model answer

    A Boolean is a data type with truth values such as true and false.

    Explanation

    Boolean values can be represented in memory using binary patterns.

  76. 76

    What is a floating-point value?

    Model answer

    A floating-point value represents a real number that may include a decimal fraction.

    Explanation

    Its precision is limited by the number of bits allocated to its representation.

  77. 77

    What is a string?

    Model answer

    A string is a sequence of characters stored using binary representations in memory.

    Explanation

    Because strings can vary in length, their implementation may require additional space or relocation.

  78. 78

    What is a set?

    Model answer

    A set is a collection of values that contains no duplicates.

    Explanation

    Its defining property is uniqueness of stored elements.

  79. 79

    What is a tuple commonly used to represent?

    Model answer

    A tuple can represent a fixed grouping of related values, such as an x,yx,yx,y coordinate or latitude and longitude.

    Explanation

    The grouped values are treated as one ordered collection.

  80. 80

    What is a range in Python?

    Model answer

    A range represents a sequence or interval of values generated according to specified bounds.

    Explanation

    It is a higher-level structure for describing a progression of values.

  81. 81

    Why are higher-level data structures needed in programming languages?

    Model answer

    They provide useful abstractions over raw bytes, allowing programmers to work with concepts such as lists, dictionaries, sets, and trees.

    Explanation

    The underlying memory still consists of binary data, but the language supplies operations that implement richer structures.

  82. 82

    What is the central trade-off illustrated by arrays, linked lists, trees, and hash tables?

    Model answer

    Faster access generally requires more structure or space, while greater flexibility often requires extra pointers or slower traversal.

    Explanation

    Choosing a data structure means balancing time, space, dynamic growth, and implementation complexity.

1 of 82
Question 1 of 82

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent