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

  • 49Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Arrays and Access

Why can the way data is arranged in memory affect an algorithm's performance?

49 questions

  1. 1

    Why can the way data is arranged in memory affect an algorithm's performance?

    1. A

      Memory representation can change how efficiently an algorithm accesses and processes data

    2. B

      Memory representation guarantees that every algorithm runs in constant time

    3. C

      Memory representation determines whether an algorithm produces logically correct results

    4. D

      Memory representation eliminates the need for an algorithm to use instructions

    Correct answer
    A

    Memory representation can change how efficiently an algorithm accesses and processes data

    Explanation

    An algorithm can be correct but inefficient. The underlying representation and layout of data affect the time and space required to execute it.

  2. 2

    What is the best general strategy for searching randomly ordered data when no additional information is available?

    1. A

      Check every second element and return to skipped elements later

    2. B

      Always check the middle element and discard half the data

    3. C

      Check elements one at a time until the desired value is found

    4. D

      Select a random element because it is usually near the target

    Correct answer
    C

    Check elements one at a time until the desired value is found

    Explanation

    With randomly ordered data, there is no basis for discarding any unchecked elements. A linear search may therefore need to inspect as many as nnn elements.

  3. 3

    Why does binary search require sorted data?

    1. A

      Sorting tells the algorithm which half cannot contain the target after each comparison

    2. B

      Sorting ensures that every target appears at the middle position

    3. C

      Sorting lets the algorithm inspect every element simultaneously

    4. D

      Sorting reduces the amount of memory required for each element

    Correct answer
    A

    Sorting tells the algorithm which half cannot contain the target after each comparison

    Explanation

    Binary search uses the ordering of the data to determine whether the target must lie to the left or right of the inspected middle element.

  4. 4

    What does contiguous storage mean in an array?

    1. A

      Values are copied into a new location after every access

    2. B

      Values are stored only in alphabetical or numerical order

    3. C

      Values are scattered randomly so that no location is reused

    4. D

      Values occupy adjacent memory locations in a predictable sequence

    Correct answer
    D

    Values occupy adjacent memory locations in a predictable sequence

    Explanation

    An array is a contiguous block of memory in which values are stored back-to-back. This predictable layout supports arithmetic indexing.

  5. 5

    Why can a computer access an array element in constant time?

    1. A

      It automatically sorts the array before performing every access

    2. B

      It searches every preceding element before reaching the requested element

    3. C

      It stores a separate copy of every element at the beginning of memory

    4. D

      It calculates the element's address from its index and jumps directly to it

    Correct answer
    D

    It calculates the element's address from its index and jumps directly to it

    Explanation

    Because array elements are equally spaced, the computer can use arithmetic on an index to calculate an element's memory address and access it directly.

  6. 6

    If an array has seven elements indexed from zero, which index identifies its final element?

    1. A

      Index 888

    2. B

      Index 777

    3. C

      Index 666

    4. D

      Index 555

    Correct answer
    C

    Index 666

    Explanation

    Zero-based indexing labels seven elements as 0,1,2,3,4,5,60,1,2,3,4,5,60,1,2,3,4,5,6, so the final index is n−1=6n-1=6n−1=6.

  7. 7

    When choosing the middle of seven indexed elements, why is index 333 used?

    1. A

      The middle index is always one less than half the array size

    2. B

      777 divided by 222 is 3.53.53.5, which is rounded down to 333

    3. C

      777 divided by 222 is 3.53.53.5, which is rounded up to 444

    4. D

      Zero-based indexing makes the middle index equal to the array size

    Correct answer
    B

    777 divided by 222 is 3.53.53.5, which is rounded down to 333

    Explanation

    The midpoint calculation uses rac{7}{2}=3.5, and rounding down gives index 333.

  8. 8

    What is the main trade-off involved in allocating extra unused array space?

    1. A

      Extra capacity may support growth but can waste memory needed elsewhere

    2. B

      Extra capacity prevents direct indexing but guarantees faster sorting

    3. C

      Extra capacity removes the need to know the data's element type

    4. D

      Extra capacity makes the array noncontiguous but reduces its memory use

    Correct answer
    A

    Extra capacity may support growth but can waste memory needed elsewhere

    Explanation

    Allocating more space than currently needed provides room for future elements, but unused memory cannot be used by other parts of the program.

  9. 9

    What does big OOO notation describe?

    1. A

      A lower bound that applies only to the algorithm's best case

    2. B

      The exact number of instructions executed by an algorithm

    3. C

      The amount of memory physically installed in a computer

    4. D

      An upper bound on an algorithm's running time or resource use

    Correct answer
    D

    An upper bound on an algorithm's running time or resource use

    Explanation

    Big OOO describes an upper bound on growth, commonly used to characterize worst-case running time.

  10. 10

    What does capital omega notation describe?

    1. A

      An upper bound that ignores the algorithm's input size

    2. B

      The number of memory addresses assigned to an array

    3. C

      The exact midpoint used during binary search

    4. D

      A lower bound on an algorithm's running time or resource use

    Correct answer
    D

    A lower bound on an algorithm's running time or resource use

    Explanation

    Capital omega, written a9 or a9(f(n)), expresses a lower bound on how quickly resource requirements grow.

  11. 11

    When can an algorithm be described using capital theta notation?

    1. A

      When its input contains exactly two possible values

    2. B

      When its best-case time is constant and its worst-case time is linear

    3. C

      When it uses an array rather than a linked data structure

    4. D

      When its upper and lower bounds have the same asymptotic growth

    Correct answer
    D

    When its upper and lower bounds have the same asymptotic growth

    Explanation

    Theta notation applies when the upper and lower asymptotic bounds coincide, giving a tight bound.

  12. 12

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

    1. A

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

    2. B

      O(log n)

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    If the target is last or absent, linear search may inspect all nnn elements, so its upper bound is O(n)O(n)O(n).

  13. 13

    What is the best-case lower bound for linear search when the target is found at the first position?

    1. A

      a9(n^2)

    2. B

      a9(n)

    3. C

      a9(log n)

    4. D

      a9(1)

    Correct answer
    D

    a9(1)

    Explanation

    If the first inspected element is the target, only a constant number of steps is needed, independent of nnn, giving a9(1).

  14. 14

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

    1. A

      The logarithmic function increases much more slowly as input size grows

    2. B

      The logarithmic function always examines every element in the input

    3. C

      The linear function requires additional memory while the logarithmic function does not

    4. D

      The logarithmic function is exact only when the input contains seven elements

    Correct answer
    A

    The logarithmic function increases much more slowly as input size grows

    Explanation

    For increasingly large nnn, log n grows far more slowly than nnn, so logarithmic algorithms scale better.

  15. 15

    What is the defining operation of bubble sort?

    1. A

      Repeatedly calculate a hash value and place each item in a bucket

    2. B

      Repeatedly divide the data into halves and merge sorted halves

    3. C

      Repeatedly choose the smallest remaining element and place it next

    4. D

      Repeatedly compare adjacent elements and swap them when they are out of order

    Correct answer
    D

    Repeatedly compare adjacent elements and swap them when they are out of order

    Explanation

    Bubble sort compares neighboring elements and swaps out-of-order pairs. Repeated passes move larger elements toward one end.

  16. 16

    Why does bubble sort repeat passes until no swaps occur?

    1. A

      A pass without swaps identifies the smallest element without comparisons

    2. B

      A pass without swaps reallocates the array into a larger memory block

    3. C

      A pass without swaps guarantees that the array contains only unique values

    4. D

      A pass without swaps shows that every adjacent pair is in the correct order

    Correct answer
    D

    A pass without swaps shows that every adjacent pair is in the correct order

    Explanation

    If a complete left-to-right pass makes no swaps, no adjacent inversion remains, so the data is sorted.

  17. 17

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

    1. A

      Bubble sort always leaves the last two elements unsorted

    2. B

      Zero-based indexing makes the final element have index n−2n-2n−2

    3. C

      Each index is compared with the next index, so the final valid starting index is n−2n-2n−2

    4. D

      The final element is never part of any comparison

    Correct answer
    C

    Each index is compared with the next index, so the final valid starting index is n−2n-2n−2

    Explanation

    The algorithm compares element iii with element i+1i+1i+1. Since the last index is n−1n-1n−1, the largest valid iii is n−2n-2n−2.

  18. 18

    What is the defining operation of selection sort?

    1. A

      Compare every adjacent pair and swap each pair that is out of order

    2. B

      Divide the input repeatedly and interleave all resulting elements

    3. C

      Find the smallest remaining element and swap it into the next final position

    4. D

      Hash each element and place it according to its first character

    Correct answer
    C

    Find the smallest remaining element and swap it into the next final position

    Explanation

    Selection sort scans the unsorted portion, selects its smallest element, and swaps it with the element at the current position.

  19. 19

    Why does selection sort inspect fewer elements on each successive pass?

    1. A

      Each pass permanently removes the largest element from memory

    2. B

      The array becomes contiguous only after the first pass

    3. C

      The elements already placed at the beginning are excluded from later searches

    4. D

      The algorithm stops comparing whenever two equal elements appear

    Correct answer
    C

    The elements already placed at the beginning are excluded from later searches

    Explanation

    After the smallest remaining element is placed, that position is sorted and does not need to be examined again.

  20. 20

    What is the typical asymptotic running time of bubble sort and selection sort?

    1. A

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

    2. B

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

    3. C

      O(log n)

    4. D

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

    Correct answer
    B

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

    Explanation

    Both algorithms perform a number of comparisons proportional to roughly nnn for roughly nnn passes, producing quadratic growth.

  21. 21

    Why does the comparison count for selection sort simplify to a quadratic order?

    1. A

      The sum contains only constant terms that become larger than n2n^2n2

    2. B

      The algorithm compares exactly n2n^2n2 pairs during every single pass

    3. C

      The algorithm performs only one comparison but stores n2n^2n2 copies of each value

    4. D

      The sum n−1+n−2+⋯+1n-1+n-2+\cdots+1n−1+n−2+⋯+1 equals n2−n2\frac{n^2-n}{2}2n2−n​, whose dominant term is n2n^2n2

    Correct answer
    D

    The sum n−1+n−2+⋯+1n-1+n-2+\cdots+1n−1+n−2+⋯+1 equals n2−n2\frac{n^2-n}{2}2n2−n​, whose dominant term is n2n^2n2

    Explanation

    The comparisons sum to n(n−1)2=n2−n2\frac{n(n-1)}{2}=\frac{n^2-n}{2}2n(n−1)​=2n2−n​. As nnn grows, the n2n^2n2 term dominates the linear term.

  22. 22

    What is the central strategy of merge sort?

    1. A

      Compare adjacent elements until the largest element bubbles upward

    2. B

      Store each element according to the first character of its name

    3. C

      Divide the data into halves, sort the halves, and merge them

    4. D

      Select the smallest element from the entire list on every pass

    Correct answer
    C

    Divide the data into halves, sort the halves, and merge them

    Explanation

    Merge sort repeatedly divides the problem, sorts smaller subproblems, and combines the sorted halves.

  23. 23

    Why does merge sort stop dividing when a subproblem contains one element?

    1. A

      A single element cannot be stored in contiguous memory

    2. B

      A single element automatically becomes the largest value

    3. C

      A single element can always be found through binary search

    4. D

      A single element is already sorted and requires no rearrangement

    Correct answer
    D

    A single element is already sorted and requires no rearrangement

    Explanation

    A one-element sequence is trivially sorted. Merge sort then combines these small sorted sequences.

  24. 24

    What is the main resource trade-off of merge sort compared with bubble sort and selection sort?

    1. A

      Merge sort avoids all comparisons but requires more input elements

    2. B

      Merge sort uses additional space to achieve faster running time

    3. C

      Merge sort uses less space but requires quadratic running time

    4. D

      Merge sort cannot operate on arrays because it needs linked nodes

    Correct answer
    B

    Merge sort uses additional space to achieve faster running time

    Explanation

    Merge sort uses temporary storage while combining subarrays, but this additional space helps reduce its time complexity.

  25. 25

    What is the typical running time of merge sort described in the content?

    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(nlog⁡n)O(n\log n)O(nlogn)

    Correct answer
    D

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

    Explanation

    Merge sort divides the data logarithmically many times while processing all nnn elements at each level, giving O(nlog⁡n)O(n\log n)O(nlogn).

  26. 26

    What does random access memory make possible?

    1. A

      Access only to bytes that are arranged alphabetically

    2. B

      Automatic sorting of all values stored in the memory module

    3. C

      Direct access to a specified byte in constant time using its address

    4. D

      Unlimited growth of every array without relocation

    Correct answer
    C

    Direct access to a specified byte in constant time using its address

    Explanation

    The term random access refers to the ability to jump directly to a particular memory address rather than proceeding sequentially.

  27. 27

    How are array indices related to memory addresses?

    1. A

      An index can be used with element size and a starting address to calculate a location

    2. B

      An index changes randomly whenever another value is inserted

    3. C

      An index is a copy of the element's binary value stored beside it

    4. D

      An index identifies only the first element because other elements are searched sequentially

    Correct answer
    A

    An index can be used with element size and a starting address to calculate a location

    Explanation

    Because array elements are equally spaced, arithmetic based on the index can locate any element's address.

  28. 28

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

    1. A

      Floating-point values cannot be represented using binary digits

    2. B

      Only a finite number of bits are available to represent its value

    3. C

      The operating system stores every floating-point value as an integer

    4. D

      Contiguous memory prevents floating-point values from containing decimals

    Correct answer
    B

    Only a finite number of bits are available to represent its value

    Explanation

    A fixed allocation such as 323232 or 646464 bits limits how many digits and fractional details can be represented.

  29. 29

    What is the main limitation of trying to append to a full contiguous array?

    1. A

      The computer cannot calculate the address of the final element

    2. B

      Appending automatically destroys the values already in the array

    3. C

      Adjacent memory may already be allocated to another part of the program

    4. D

      The array cannot store values of the same type after initialization

    Correct answer
    C

    Adjacent memory may already be allocated to another part of the program

    Explanation

    If the immediately following memory is occupied, the array cannot grow in place and may need to be moved to a larger contiguous region.

  30. 30

    What is the cost of relocating an array to a larger memory region?

    1. A

      Only the array's first element must be moved because indices are unchanged

    2. B

      The operating system can enlarge the array without touching any data

    3. C

      Every existing element must be copied or moved to the new region

    4. D

      Relocation changes the values but does not require additional time

    Correct answer
    C

    Every existing element must be copied or moved to the new region

    Explanation

    Moving the array requires transferring all stored values to the new contiguous region, which takes time proportional to the amount of data.

  31. 31

    How does a linked list support growth in nonadjacent memory?

    1. A

      The list sorts values so that their addresses can be inferred from their sizes

    2. B

      Each node is placed next to the previous node without storing any metadata

    3. C

      Each node stores a value and a pointer to the address of the next node

    4. D

      The list reserves all possible future positions before storing its first value

    Correct answer
    C

    Each node stores a value and a pointer to the address of the next node

    Explanation

    Nodes can be scattered in memory, but each node stores the address of the next node, allowing the structure to be traversed.

  32. 32

    What does a null or zero pointer at the end of a linked list indicate?

    1. A

      The current node has two children in a tree

    2. B

      The current node contains the smallest value in the list

    3. C

      The current node must be moved to the beginning of memory

    4. D

      There is no next node after the current node

    Correct answer
    D

    There is no next node after the current node

    Explanation

    The final node uses a special convention, often a null or all-zero address, to indicate that traversal should stop.

  33. 33

    What is a major disadvantage of linked lists compared with arrays?

    1. A

      They do not support direct random access to an arbitrary position

    2. B

      They cannot grow after their initial allocation

    3. C

      They cannot remove an element without sorting the entire structure

    4. D

      They require all nodes to be stored contiguously

    Correct answer
    A

    They do not support direct random access to an arbitrary position

    Explanation

    A linked list must usually be traversed from its first node, so reaching a middle or later node can require linear time.

  34. 34

    Why does a linked list require more space than an array storing the same values?

    1. A

      Each node stores the binary representation of every memory address

    2. B

      Each node stores both its data and a pointer to another node

    3. C

      Each node stores a complete duplicate of the entire list

    4. D

      Each node must reserve space for every possible future value

    Correct answer
    B

    Each node stores both its data and a pointer to another node

    Explanation

    Linked-list nodes contain metadata, such as a next-node address, in addition to the data value.

  35. 35

    What operation can remove a middle node from a linked list efficiently once its predecessor is known?

    1. A

      Shift every later node one position toward the beginning

    2. B

      Update the predecessor's pointer to skip over the removed node

    3. C

      Sort all remaining nodes before reconnecting them

    4. D

      Reallocate the entire list into one contiguous memory block

    Correct answer
    B

    Update the predecessor's pointer to skip over the removed node

    Explanation

    Removing a node can be done by changing the preceding node's pointer to refer to the removed node's successor.

  36. 36

    What property defines a binary tree?

    1. A

      Each node has no more than two children

    2. B

      Each node stores exactly two values and no pointers

    3. C

      Every node must have two children and two parents

    4. D

      The tree must contain exactly two levels

    Correct answer
    A

    Each node has no more than two children

    Explanation

    A binary tree permits each node to have zero, one, or two children.

  37. 37

    What ordering property defines a binary search tree?

    1. A

      Values are stored according to the order in which they were inserted

    2. B

      Every node is larger than all nodes below it in both subtrees

    3. C

      Values alternate between odd and even levels throughout the tree

    4. D

      Values in the left subtree are smaller and values in the right subtree are larger than the node

    Correct answer
    D

    Values in the left subtree are smaller and values in the right subtree are larger than the node

    Explanation

    For each node in a binary search tree, values reachable in the left subtree are smaller and values in the right subtree are larger.

  38. 38

    Why can a binary search tree support logarithmic-time search when well balanced?

    1. A

      The tree stores every value in a contiguous sequence of addresses

    2. B

      The tree eliminates the need to compare the target with stored values

    3. C

      Each comparison can select one of two subtrees and discard the other

    4. D

      Each node contains a complete index of all values in the tree

    Correct answer
    C

    Each comparison can select one of two subtrees and discard the other

    Explanation

    At each node, the ordering property determines whether to continue left or right, reducing the remaining search region by about half when balanced.

  39. 39

    What can happen to a binary search tree if values are inserted in an unfavorable order?

    1. A

      It can become elongated and behave similarly to a linked list

    2. B

      It automatically becomes a complete tree with equal-height branches

    3. C

      It becomes an array and no longer requires pointers

    4. D

      It loses all stored values because its root must be replaced

    Correct answer
    A

    It can become elongated and behave similarly to a linked list

    Explanation

    If values repeatedly extend one side of the tree, the structure can become unbalanced and search may degrade to linear time.

  40. 40

    What is the main trade-off associated with tree structures?

    1. A

      They require sorting after every search but never need rebalancing

    2. B

      They use less space than arrays and always provide constant-time access

    3. C

      They eliminate pointer space but require all values to remain contiguous

    4. D

      They use pointer space to support flexible organization and potentially faster searches

    Correct answer
    D

    They use pointer space to support flexible organization and potentially faster searches

    Explanation

    Tree nodes use additional pointers, but their organization can reduce search time compared with a linked list.

  41. 41

    What is the purpose of a hash function in a hash table?

    1. A

      It sorts every key by comparing it with every other key

    2. B

      It converts a key into an index or bucket location

    3. C

      It stores all keys in one contiguous linked-list node

    4. D

      It converts values into pointers without using any indexes

    Correct answer
    B

    It converts a key into an index or bucket location

    Explanation

    A hash function maps an input key to an array position, allowing the table to locate the relevant bucket quickly.

  42. 42

    In the example using the first letter of a name, how could the name David be assigned to an array index?

    1. A

      Multiply the length of David by the number of alphabetic letters

    2. B

      Place David at the index equal to the number of letters in the name

    3. C

      Convert DDD to its character code and subtract the code for AAA

    4. D

      Sort all names first and use David's position in the sorted list

    Correct answer
    C

    Convert DDD to its character code and subtract the code for AAA

    Explanation

    Using character codes, subtracting the code for AAA from the code for DDD maps DDD to the zero-based index 333.

  43. 43

    What is a collision in a hash table?

    1. A

      A key is stored at two different indexes at the same time

    2. B

      The hash function produces no index for a valid key

    3. C

      Different keys produce the same hash-table index

    4. D

      Two arrays contain identical memory addresses by design

    Correct answer
    C

    Different keys produce the same hash-table index

    Explanation

    A collision occurs when multiple inputs map to the same bucket or index.

  44. 44

    How can separate chaining handle multiple names that hash to the same bucket?

    1. A

      Discard every entry except the first one inserted into the bucket

    2. B

      Replace the hash table with a sorted array after every collision

    3. C

      Expand the alphabet so that every name receives a unique first letter

    4. D

      Store the colliding entries in a linked structure associated with that bucket

    Correct answer
    D

    Store the colliding entries in a linked structure associated with that bucket

    Explanation

    Separate chaining uses a linked list or similar structure at each bucket to hold multiple entries with the same hash result.

  45. 45

    Why is a hash table only approximately constant time in general?

    1. A

      Hash functions always examine every key before returning an index

    2. B

      Collisions can create bucket lists that require additional searching

    3. C

      Every hash-table operation requires sorting all stored values

    4. D

      Hash tables cannot use array indexing to access buckets

    Correct answer
    B

    Collisions can create bucket lists that require additional searching

    Explanation

    With a good hash function and few collisions, access is near constant time. Long collision chains can make operations slower, potentially linear.

  46. 46

    What is a dictionary in Python conceptually?

    1. A

      A binary search tree restricted to single-character keys

    2. B

      A hash table that associates keys with values

    3. C

      A linked list that permits duplicate keys without values

    4. D

      A sorted array that stores only numerical indexes

    Correct answer
    B

    A hash table that associates keys with values

    Explanation

    A Python dictionary provides an abstraction for mapping keys, such as names, to associated values.

  47. 47

    What is a Python set designed to represent?

    1. A

      A pair of related coordinates such as latitude and longitude

    2. B

      A sequence whose elements must remain contiguous in memory

    3. C

      A mapping in which every key must have two associated values

    4. D

      A collection of values with no duplicates

    Correct answer
    D

    A collection of values with no duplicates

    Explanation

    A set is a collection that contains unique values rather than repeated copies of the same value.

  48. 48

    What is a tuple particularly suitable for representing?

    1. A

      A fixed grouping of related values such as an x,yx,yx,y coordinate

    2. B

      A hash table whose keys are always alphabetic characters

    3. C

      A sequence that must be repeatedly sorted after each insertion

    4. D

      A dynamically growing collection with arbitrary duplicate values

    Correct answer
    A

    A fixed grouping of related values such as an x,yx,yx,y coordinate

    Explanation

    Tuples can represent grouped values, such as (x,y)(x,y)(x,y) or latitude and longitude.

  49. 49

    How is a Python list conceptually related to an array?

    1. A

      It stores values only as key-value pairs accessed through hashing

    2. B

      It is a binary search tree whose nodes have at most two children

    3. C

      It prevents direct indexing by storing every element at a random address

    4. D

      It provides array-like indexed storage while supporting dynamic growth and shrinking

    Correct answer
    D

    It provides array-like indexed storage while supporting dynamic growth and shrinking

    Explanation

    A list provides the array abstraction while managing memory so the collection can change size more dynamically.

Question 1 of 49
Question 1 of 49

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent