1. Home
  2. Explore
  3. Computer Science
  4. Week 3: Algorithms

Study methods

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

Week 3: Algorithms

  • 44Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Searching and Asymptotic Notation

What name is given to the divide-and-conquer algorithm used in the phonebook example?

44 questions

  1. 1

    What name is given to the divide-and-conquer algorithm used in the phonebook example?

    1. A

      Bubble sort

    2. B

      Selection sort

    3. C

      Binary search

    4. D

      Linear search

    Correct answer
    C

    Binary search

    Explanation

    The divide-and-conquer algorithm from the phonebook example is called binary search, as it repeatedly splits the search space in half.

  2. 2

    In linear search, how does the algorithm proceed through an array?

    1. A

      It starts at the leftmost element and goes right until it finds the target or reaches the end.

    2. B

      It starts at the middle and moves outward.

    3. C

      It randomly selects elements until the target is found.

    4. D

      It starts at both ends and moves inward.

    Correct answer
    A

    It starts at the leftmost element and goes right until it finds the target or reaches the end.

    Explanation

    Linear search checks each element from left to right (or from the beginning to the end) sequentially.

  3. 3

    What is a necessary condition for binary search to work correctly?

    1. A

      The data must be stored in a linked list.

    2. B

      The data must be in reverse order.

    3. C

      The data must be sorted.

    4. D

      The data must have fewer than 100 elements.

    Correct answer
    C

    The data must be sorted.

    Explanation

    Binary search requires the array to be sorted so that it can decide which half to discard based on the comparison.

  4. 4

    Where should 'return false' be placed in a linear search pseudocode loop?

    1. A

      Inside the if clause alongside 'return true'

    2. B

      After the loop ends (outside the loop)

    3. C

      Inside the else clause of the if statement

    4. D

      Before the loop starts

    Correct answer
    B

    After the loop ends (outside the loop)

    Explanation

    Returning false should occur after the loop ends, meaning the value was not found in any element; placing it inside the else clause would cause premature termination.

  5. 5

    In the binary search pseudocode, what do the recursive calls do?

    1. A

      Double the search space

    2. B

      Search only the left half or the right half of the current portion

    3. C

      Terminate the program if the element is not found

    4. D

      Search the entire array again

    Correct answer
    B

    Search only the left half or the right half of the current portion

    Explanation

    The recursive calls focus on either the left half or the right half of the current search interval, narrowing down the search.

  6. 6

    What does the big O notation describe about an algorithm's running time?

    1. A

      The average-case running time

    2. B

      An upper bound on the running time as input size grows

    3. C

      The exact number of steps for a given input

    4. D

      The best-case running time

    Correct answer
    B

    An upper bound on the running time as input size grows

    Explanation

    Big O notation gives an upper bound (worst-case) on the growth rate of an algorithm's running time relative to input size.

  7. 7

    Which of the following is a common big O running time?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    O(nlog⁡n)O(n \log n)O(nlogn) is a common running time for efficient sorting algorithms like mergesort, while the others are less common in typical introductory algorithms.

  8. 8

    What does the big Omega notation represent?

    1. A

      A lower bound on running time

    2. B

      The average running time

    3. C

      An upper bound on running time

    4. D

      The exact running time

    Correct answer
    A

    A lower bound on running time

    Explanation

    Big Omega provides a lower bound (best-case) on the running time of an algorithm.

  9. 9

    When do we use the big Theta notation for an algorithm?

    1. A

      When the algorithm has different upper and lower bounds

    2. B

      When the algorithm is not efficient

    3. C

      When the algorithm can only run in the best case

    4. D

      When the algorithm has the same upper and lower bounds

    Correct answer
    D

    When the algorithm has the same upper and lower bounds

    Explanation

    Big Theta is used when the upper bound (big O) and lower bound (big Omega) are the same, meaning the algorithm's running time is tightly bounded.

  10. 10

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

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    C

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

    Explanation

    In the worst case, linear search must check all n elements, so its worst-case running time is O(n)O(n)O(n).

  11. 11

    In the best case, linear search runs in what time?

    1. A

      Ω(log⁡n)\Omega(\log n)Ω(logn)

    2. B

      Ω(1)\Omega(1)Ω(1)

    3. C

      Ω(n2)\Omega(n^2)Ω(n2)

    4. D

      Ω(n)\Omega(n)Ω(n)

    Correct answer
    B

    Ω(1)\Omega(1)Ω(1)

    Explanation

    If the target is at the first position, linear search finds it in constant time, so the best case is Ω(1)\Omega(1)Ω(1).

  12. 12

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

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Binary search halves the search space each step, so it takes at most log⁡2n\log_2 nlog2​n steps in the worst case, giving O(log⁡n)O(\log n)O(logn).

  13. 13

    What is the best-case running time of binary search?

    1. A

      Ω(log⁡n)\Omega(\log n)Ω(logn)

    2. B

      Ω(n)\Omega(n)Ω(n)

    3. C

      Ω(1)\Omega(1)Ω(1)

    4. D

      Ω(n2)\Omega(n^2)Ω(n2)

    Correct answer
    C

    Ω(1)\Omega(1)Ω(1)

    Explanation

    If the middle element is the target on the first check, binary search takes constant time, so best case is Ω(1)\Omega(1)Ω(1).

  14. 14

    What is the effect of placing 'return false' inside the else clause of a linear search loop?

    1. A

      The algorithm will crash.

    2. B

      The algorithm will incorrectly report that the target is not found after checking only the first element.

    3. C

      The algorithm will find the target faster.

    4. D

      The algorithm will check all elements and then return false.

    Correct answer
    B

    The algorithm will incorrectly report that the target is not found after checking only the first element.

    Explanation

    If 'return false' is inside the else clause, the loop will exit after the first element if it doesn't match, leading to incorrect results.

  15. 15

    What is the first step in each pass of selection sort?

    1. A

      Swap the first two elements if they are out of order.

    2. B

      Compare adjacent elements from left to right.

    3. C

      Select a random element to place at the end.

    4. D

      Find the smallest element in the unsorted portion of the array.

    Correct answer
    D

    Find the smallest element in the unsorted portion of the array.

    Explanation

    Selection sort first scans the unsorted portion to find the smallest element, then swaps it into the correct position.

  16. 16

    In selection sort, what does the outer loop iterate over?

    1. A

      From the first element to the second-to-last

    2. B

      From the first element to the last

    3. C

      From the second element to the last

    4. D

      From the last element to the first

    Correct answer
    B

    From the first element to the last

    Explanation

    The outer loop iterates from the first index (0) to the last index (n-1), each time placing the next smallest element into its correct position.

  17. 17

    What is the worst-case time complexity of selection sort?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    A

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

    Explanation

    Selection sort makes approximately n2/2n^2/2n2/2 comparisons, so its worst-case complexity is O(n2)O(n^2)O(n2).

  18. 18

    What is the lower bound on selection sort's running time?

    1. A

      Ω(n2)\Omega(n^2)Ω(n2)

    2. B

      Ω(nlog⁡n)\Omega(n \log n)Ω(nlogn)

    3. C

      Ω(n)\Omega(n)Ω(n)

    4. D

      Ω(1)\Omega(1)Ω(1)

    Correct answer
    A

    Ω(n2)\Omega(n^2)Ω(n2)

    Explanation

    Even in the best case, selection sort always performs the same number of comparisons, so its lower bound is also Ω(n2)\Omega(n^2)Ω(n2), making it Θ(n2)\Theta(n^2)Θ(n2).

  19. 19

    In bubble sort, what is the basic operation in each pass?

    1. A

      Merge two sorted halves of the array.

    2. B

      Find the smallest element and swap it with the current position.

    3. C

      Compare adjacent elements and swap them if they are in the wrong order.

    4. D

      Move the largest element to the beginning.

    Correct answer
    C

    Compare adjacent elements and swap them if they are in the wrong order.

    Explanation

    Bubble sort repeatedly compares adjacent elements and swaps them if they are out of order, causing larger elements to 'bubble' to the end.

  20. 20

    In the bubble sort pseudocode, the inner loop goes from 0 to what value?

    1. A

      n−2n-2n−2

    2. B

      n/2n/2n/2

    3. C

      nnn

    4. D

      n−1n-1n−1

    Correct answer
    A

    n−2n-2n−2

    Explanation

    The inner loop goes from 0 to n−2n-2n−2 because it compares each element with the next one; the last element has no neighbor beyond index n−1n-1n−1.

  21. 21

    How can bubble sort be optimized to run faster on an already-sorted array?

    1. A

      By checking if any swaps were made in a pass and stopping early if none were made

    2. B

      By starting from the end of the array

    3. C

      By using recursion

    4. D

      By using more memory

    Correct answer
    A

    By checking if any swaps were made in a pass and stopping early if none were made

    Explanation

    If no swaps occur during a pass, the array is already sorted, so the algorithm can terminate early, improving the best-case to Ω(n)\Omega(n)Ω(n).

  22. 22

    What is the worst-case time complexity of bubble sort?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    B

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

    Explanation

    In the worst case (reverse order), bubble sort requires approximately n2n^2n2 comparisons and swaps, so it is O(n2)O(n^2)O(n2).

  23. 23

    With the optimization, what is the best-case running time of bubble sort?

    1. A

      Ω(1)\Omega(1)Ω(1)

    2. B

      Ω(nlog⁡n)\Omega(n \log n)Ω(nlogn)

    3. C

      Ω(n2)\Omega(n^2)Ω(n2)

    4. D

      Ω(n)\Omega(n)Ω(n)

    Correct answer
    D

    Ω(n)\Omega(n)Ω(n)

    Explanation

    If the array is already sorted, the optimization detects no swaps in the first pass and stops, so only n−1n-1n−1 comparisons are made, giving Ω(n)\Omega(n)Ω(n).

  24. 24

    What are the three steps of the mergesort algorithm?

    1. A

      Compare adjacent, swap, repeat

    2. B

      Divide, conquer, combine

    3. C

      Sort left half, sort right half, merge sorted halves

    4. D

      Find smallest, swap, repeat

    Correct answer
    C

    Sort left half, sort right half, merge sorted halves

    Explanation

    Mergesort recursively sorts the left half, then the right half, and finally merges the two sorted halves together.

  25. 25

    What is the time complexity of mergesort?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    B

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

    Explanation

    Mergesort divides the list in half each time (log n levels) and merges n elements per level, resulting in O(nlog⁡n)O(n \log n)O(nlogn) time complexity.

  26. 26

    What is the lower bound on mergesort's running time?

    1. A

      Ω(n2)\Omega(n^2)Ω(n2)

    2. B

      Ω(log⁡n)\Omega(\log n)Ω(logn)

    3. C

      Ω(nlog⁡n)\Omega(n \log n)Ω(nlogn)

    4. D

      Ω(n)\Omega(n)Ω(n)

    Correct answer
    C

    Ω(nlog⁡n)\Omega(n \log n)Ω(nlogn)

    Explanation

    Mergesort always performs the same number of comparisons regardless of input order, so its lower bound is also Ω(nlog⁡n)\Omega(n \log n)Ω(nlogn), making it Θ(nlog⁡n)\Theta(n \log n)Θ(nlogn).

  27. 27

    How many steps does it take to merge two sorted lists of total nnn elements?

    1. A

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

    2. B

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

    3. C

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

    4. D

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

    Correct answer
    B

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

    Explanation

    Merging two sorted lists requires examining each element exactly once, so it takes O(n)O(n)O(n) steps.

  28. 28

    What is one trade-off of mergesort compared to selection sort?

    1. A

      Mergesort cannot handle large arrays.

    2. B

      Mergesort is easier to implement.

    3. C

      Mergesort has worse time complexity.

    4. D

      Mergesort uses more memory.

    Correct answer
    D

    Mergesort uses more memory.

    Explanation

    Mergesort requires additional memory for the temporary arrays during merging, whereas selection sort sorts in place.

  29. 29

    What does it mean for an algorithm to be recursive?

    1. A

      It is implemented in a recursive data structure.

    2. B

      It uses iteration (loops) to solve a problem.

    3. C

      It calls itself to solve smaller instances of the same problem.

    4. D

      It uses more than one function.

    Correct answer
    C

    It calls itself to solve smaller instances of the same problem.

    Explanation

    A recursive algorithm solves a problem by calling itself on smaller or simpler input until a base case is reached.

  30. 30

    In a recursive function, what is a base case?

    1. A

      The case that triggers the recursive call.

    2. B

      The case that is called first.

    3. C

      The case that uses the most memory.

    4. D

      The case that stops the recursion and returns a result directly.

    Correct answer
    D

    The case that stops the recursion and returns a result directly.

    Explanation

    A base case is a simple input for which the answer is known directly, preventing infinite recursion.

  31. 31

    What characterizes a recursive case in a recursive algorithm?

    1. A

      It performs no further calls.

    2. B

      It uses a loop instead of a function call.

    3. C

      It reduces the problem to a smaller or simpler instance of the same problem.

    4. D

      It increases the problem size.

    Correct answer
    C

    It reduces the problem to a smaller or simpler instance of the same problem.

    Explanation

    A recursive case breaks the problem down into a smaller version of the same problem, eventually reaching the base case.

  32. 32

    In the recursive pyramid example, a pyramid of height 4 is defined as what?

    1. A

      A pyramid of height 5 minus a row

    2. B

      A single block repeated 4 times

    3. C

      A pyramid of height 3 plus an additional row of 4 blocks

    4. D

      A pyramid of height 2 times itself

    Correct answer
    C

    A pyramid of height 3 plus an additional row of 4 blocks

    Explanation

    A pyramid of height 4 is built by first drawing a pyramid of height 3, then adding a row of 4 blocks at the bottom.

  33. 33

    What is the base case in the recursive drawing of a pyramid?

    1. A

      A height of 1 draws one block.

    2. B

      A height of 0 draws nothing (just returns).

    3. C

      A height of 2 draws two rows.

    4. D

      A height of -1 resets the drawing.

    Correct answer
    B

    A height of 0 draws nothing (just returns).

    Explanation

    The base case is height 0, which does nothing and stops the recursion; this is the simplest case.

  34. 34

    Which function should be used to compare two strings in C?

    1. A

      strcmp

    2. B

      strcat

    3. C

      strcpy

    4. D

      strlen

    Correct answer
    A

    strcmp

    Explanation

    The strcmp function compares two strings and returns an integer indicating their order or equality.

  35. 35

    What does strcmp return when two strings are equal?

    1. A

      The length of the strings

    2. B

      0

    3. C

      1

    4. D

      -1

    Correct answer
    B

    0

    Explanation

    strcmp returns 0 if the two strings are identical character by character.

  36. 36

    How do you create a custom data type containing a name and a number in C?

    1. A

      Using a struct with typedef

    2. B

      Using a union

    3. C

      Using a function pointer

    4. D

      Using an array of strings

    Correct answer
    A

    Using a struct with typedef

    Explanation

    You can define a struct (e.g., typedef struct { string name; string number; } person;) to group related data into a custom type.

  37. 37

    What operator is used to access a member of a struct?

    1. A

      Dot operator (.)

    2. B

      Colon operator (::)

    3. C

      Pointer operator (*)

    4. D

      Arrow operator (->)

    Correct answer
    A

    Dot operator (.)

    Explanation

    The dot operator is used to access members of a struct directly, e.g., person.name.

  38. 38

    What is a drawback of using parallel arrays to store related data?

    1. A

      They require more memory than an array of structs.

    2. B

      They cannot store strings.

    3. C

      They are slower to access than a single array.

    4. D

      It is easy to accidentally misalign the data between arrays.

    Correct answer
    D

    It is easy to accidentally misalign the data between arrays.

    Explanation

    Parallel arrays require the programmer to manually keep them in sync; any insertion or deletion can break the correspondence.

  39. 39

    Why are magic numbers (hardcoded sizes) considered bad practice?

    1. A

      They make the code run slower.

    2. B

      They reduce code readability and maintainability.

    3. C

      They prevent the use of arrays.

    4. D

      They cause syntax errors.

    Correct answer
    B

    They reduce code readability and maintainability.

    Explanation

    Magic numbers make the code harder to understand and modify; if the size changes, it must be updated in multiple places.

  40. 40

    In C, what does returning 0 from main typically indicate?

    1. A

      The program ran successfully

    2. B

      The program is still running

    3. C

      An error occurred

    4. D

      The program encountered a crash

    Correct answer
    A

    The program ran successfully

    Explanation

    By convention, returning 0 from main signals that the program executed successfully, while non-zero values indicate errors.

  41. 41

    How can you initialize an array with specific values in C?

    1. A

      Arrays cannot be initialized with specific values

    2. B

      By assigning each element individually after declaration

    3. C

      By using curly braces during declaration, e.g., int arr[] = {1,2,3};

    4. D

      By using an assignment statement with a loop

    Correct answer
    C

    By using curly braces during declaration, e.g., int arr[] = {1,2,3};

    Explanation

    C allows initializing an array at the time of declaration using comma-separated values inside curly braces.

  42. 42

    Which header file must be included to use the strcmp function?

    1. A

      <string.h>

    2. B

      <stdlib.h>

    3. C

      <stdio.h>

    4. D

      <cs50.h>

    Correct answer
    A

    <string.h>

    Explanation

    The strcmp function is declared in <string.h>, so that header must be included.

  43. 43

    If an array has nnn elements, what is the index of the last element?

    1. A

      n−1n-1n−1

    2. B

      n+1n+1n+1

    3. C

      nnn

    4. D

      n/2n/2n/2

    Correct answer
    A

    n−1n-1n−1

    Explanation

    C arrays are zero-indexed, so the last element is at index n−1n-1n−1.

  44. 44

    What does it mean for an array to be 'contiguous' in memory?

    1. A

      The array elements are stored in reverse order.

    2. B

      The array elements are stored next to each other without gaps.

    3. C

      The array elements are stored in separate memory regions.

    4. D

      The array elements are stored in random locations.

    Correct answer
    B

    The array elements are stored next to each other without gaps.

    Explanation

    A contiguous array has its elements placed consecutively in memory, allowing efficient access by index.

Question 1 of 44
Question 1 of 44

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent