Week 3: Algorithms
- 44Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What name is given to the divide-and-conquer algorithm used in the phonebook example?
What name is given to the divide-and-conquer algorithm used in the phonebook example?
44 questions
- 1
What name is given to the divide-and-conquer algorithm used in the phonebook example?
- A
Bubble sort
- B
Selection sort
- C
Binary search
- D
Linear search
Correct answer
CBinary search
Explanation
The divide-and-conquer algorithm from the phonebook example is called binary search, as it repeatedly splits the search space in half.
- A
- 2
In linear search, how does the algorithm proceed through an array?
- A
It starts at the leftmost element and goes right until it finds the target or reaches the end.
- B
It starts at the middle and moves outward.
- C
It randomly selects elements until the target is found.
- D
It starts at both ends and moves inward.
Correct answer
AIt 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.
- A
- 3
What is a necessary condition for binary search to work correctly?
- A
The data must be stored in a linked list.
- B
The data must be in reverse order.
- C
The data must be sorted.
- D
The data must have fewer than 100 elements.
Correct answer
CThe 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.
- A
- 4
Where should 'return false' be placed in a linear search pseudocode loop?
- A
Inside the if clause alongside 'return true'
- B
After the loop ends (outside the loop)
- C
Inside the else clause of the if statement
- D
Before the loop starts
Correct answer
BAfter 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.
- A
- 5
In the binary search pseudocode, what do the recursive calls do?
- A
Double the search space
- B
Search only the left half or the right half of the current portion
- C
Terminate the program if the element is not found
- D
Search the entire array again
Correct answer
BSearch 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.
- A
- 6
What does the big O notation describe about an algorithm's running time?
- A
The average-case running time
- B
An upper bound on the running time as input size grows
- C
The exact number of steps for a given input
- D
The best-case running time
Correct answer
BAn 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.
- A
- 7
Which of the following is a common big O running time?
- A
O(nlogn)
- B
O(2n)
- C
O(n3)
- D
O(n!)
Correct answer
AO(nlogn)
Explanation
O(nlogn) is a common running time for efficient sorting algorithms like mergesort, while the others are less common in typical introductory algorithms.
- A
- 8
What does the big Omega notation represent?
- A
A lower bound on running time
- B
The average running time
- C
An upper bound on running time
- D
The exact running time
Correct answer
AA lower bound on running time
Explanation
Big Omega provides a lower bound (best-case) on the running time of an algorithm.
- A
- 9
When do we use the big Theta notation for an algorithm?
- A
When the algorithm has different upper and lower bounds
- B
When the algorithm is not efficient
- C
When the algorithm can only run in the best case
- D
When the algorithm has the same upper and lower bounds
Correct answer
DWhen 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.
- A
- 10
What is the worst-case running time of linear search?
- A
O(1)
- B
O(logn)
- C
O(n)
- D
O(n2)
Correct answer
CO(n)
Explanation
In the worst case, linear search must check all n elements, so its worst-case running time is O(n).
- A
- 11
In the best case, linear search runs in what time?
- A
Ω(logn)
- B
Ω(1)
- C
Ω(n2)
- D
Ω(n)
Correct answer
BΩ(1)
Explanation
If the target is at the first position, linear search finds it in constant time, so the best case is Ω(1).
- A
- 12
What is the worst-case running time of binary search?
- A
O(logn)
- B
O(1)
- C
O(n)
- D
O(nlogn)
Correct answer
AO(logn)
Explanation
Binary search halves the search space each step, so it takes at most log2n steps in the worst case, giving O(logn).
- A
- 13
What is the best-case running time of binary search?
- A
Ω(logn)
- B
Ω(n)
- C
Ω(1)
- D
Ω(n2)
Correct answer
CΩ(1)
Explanation
If the middle element is the target on the first check, binary search takes constant time, so best case is Ω(1).
- A
- 14
What is the effect of placing 'return false' inside the else clause of a linear search loop?
- A
The algorithm will crash.
- B
The algorithm will incorrectly report that the target is not found after checking only the first element.
- C
The algorithm will find the target faster.
- D
The algorithm will check all elements and then return false.
Correct answer
BThe 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.
- A
- 15
What is the first step in each pass of selection sort?
- A
Swap the first two elements if they are out of order.
- B
Compare adjacent elements from left to right.
- C
Select a random element to place at the end.
- D
Find the smallest element in the unsorted portion of the array.
Correct answer
DFind 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.
- A
- 16
In selection sort, what does the outer loop iterate over?
- A
From the first element to the second-to-last
- B
From the first element to the last
- C
From the second element to the last
- D
From the last element to the first
Correct answer
BFrom 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.
- A
- 17
What is the worst-case time complexity of selection sort?
- A
O(n2)
- B
O(logn)
- C
O(nlogn)
- D
O(n)
Correct answer
AO(n2)
Explanation
Selection sort makes approximately n2/2 comparisons, so its worst-case complexity is O(n2).
- A
- 18
What is the lower bound on selection sort's running time?
- A
Ω(n2)
- B
Ω(nlogn)
- C
Ω(n)
- D
Ω(1)
Correct answer
AΩ(n2)
Explanation
Even in the best case, selection sort always performs the same number of comparisons, so its lower bound is also Ω(n2), making it Θ(n2).
- A
- 19
In bubble sort, what is the basic operation in each pass?
- A
Merge two sorted halves of the array.
- B
Find the smallest element and swap it with the current position.
- C
Compare adjacent elements and swap them if they are in the wrong order.
- D
Move the largest element to the beginning.
Correct answer
CCompare 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.
- A
- 20
In the bubble sort pseudocode, the inner loop goes from 0 to what value?
- A
n−2
- B
n/2
- C
n
- D
n−1
Correct answer
An−2
Explanation
The inner loop goes from 0 to n−2 because it compares each element with the next one; the last element has no neighbor beyond index n−1.
- A
- 21
How can bubble sort be optimized to run faster on an already-sorted array?
- A
By checking if any swaps were made in a pass and stopping early if none were made
- B
By starting from the end of the array
- C
By using recursion
- D
By using more memory
Correct answer
ABy 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).
- A
- 22
What is the worst-case time complexity of bubble sort?
- A
O(nlogn)
- B
O(n2)
- C
O(n)
- D
O(logn)
Correct answer
BO(n2)
Explanation
In the worst case (reverse order), bubble sort requires approximately n2 comparisons and swaps, so it is O(n2).
- A
- 23
With the optimization, what is the best-case running time of bubble sort?
- A
Ω(1)
- B
Ω(nlogn)
- C
Ω(n2)
- D
Ω(n)
Correct answer
DΩ(n)
Explanation
If the array is already sorted, the optimization detects no swaps in the first pass and stops, so only n−1 comparisons are made, giving Ω(n).
- A
- 24
What are the three steps of the mergesort algorithm?
- A
Compare adjacent, swap, repeat
- B
Divide, conquer, combine
- C
Sort left half, sort right half, merge sorted halves
- D
Find smallest, swap, repeat
Correct answer
CSort 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.
- A
- 25
What is the time complexity of mergesort?
- A
O(n2)
- B
O(nlogn)
- C
O(n)
- D
O(logn)
Correct answer
BO(nlogn)
Explanation
Mergesort divides the list in half each time (log n levels) and merges n elements per level, resulting in O(nlogn) time complexity.
- A
- 26
What is the lower bound on mergesort's running time?
- A
Ω(n2)
- B
Ω(logn)
- C
Ω(nlogn)
- D
Ω(n)
Correct answer
CΩ(nlogn)
Explanation
Mergesort always performs the same number of comparisons regardless of input order, so its lower bound is also Ω(nlogn), making it Θ(nlogn).
- A
- 27
How many steps does it take to merge two sorted lists of total n elements?
- A
O(n2)
- B
O(n)
- C
O(1)
- D
O(logn)
Correct answer
BO(n)
Explanation
Merging two sorted lists requires examining each element exactly once, so it takes O(n) steps.
- A
- 28
What is one trade-off of mergesort compared to selection sort?
- A
Mergesort cannot handle large arrays.
- B
Mergesort is easier to implement.
- C
Mergesort has worse time complexity.
- D
Mergesort uses more memory.
Correct answer
DMergesort uses more memory.
Explanation
Mergesort requires additional memory for the temporary arrays during merging, whereas selection sort sorts in place.
- A
- 29
What does it mean for an algorithm to be recursive?
- A
It is implemented in a recursive data structure.
- B
It uses iteration (loops) to solve a problem.
- C
It calls itself to solve smaller instances of the same problem.
- D
It uses more than one function.
Correct answer
CIt 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.
- A
- 30
In a recursive function, what is a base case?
- A
The case that triggers the recursive call.
- B
The case that is called first.
- C
The case that uses the most memory.
- D
The case that stops the recursion and returns a result directly.
Correct answer
DThe 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.
- A
- 31
What characterizes a recursive case in a recursive algorithm?
- A
It performs no further calls.
- B
It uses a loop instead of a function call.
- C
It reduces the problem to a smaller or simpler instance of the same problem.
- D
It increases the problem size.
Correct answer
CIt 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.
- A
- 32
In the recursive pyramid example, a pyramid of height 4 is defined as what?
- A
A pyramid of height 5 minus a row
- B
A single block repeated 4 times
- C
A pyramid of height 3 plus an additional row of 4 blocks
- D
A pyramid of height 2 times itself
Correct answer
CA 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.
- A
- 33
What is the base case in the recursive drawing of a pyramid?
- A
A height of 1 draws one block.
- B
A height of 0 draws nothing (just returns).
- C
A height of 2 draws two rows.
- D
A height of -1 resets the drawing.
Correct answer
BA 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.
- A
- 34
Which function should be used to compare two strings in C?
- A
strcmp
- B
strcat
- C
strcpy
- D
strlen
Correct answer
Astrcmp
Explanation
The strcmp function compares two strings and returns an integer indicating their order or equality.
- A
- 35
What does strcmp return when two strings are equal?
- A
The length of the strings
- B
0
- C
1
- D
-1
Correct answer
B0
Explanation
strcmp returns 0 if the two strings are identical character by character.
- A
- 36
How do you create a custom data type containing a name and a number in C?
- A
Using a struct with typedef
- B
Using a union
- C
Using a function pointer
- D
Using an array of strings
Correct answer
AUsing 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.
- A
- 37
What operator is used to access a member of a struct?
- A
Dot operator (.)
- B
Colon operator (::)
- C
Pointer operator (*)
- D
Arrow operator (->)
Correct answer
ADot operator (.)
Explanation
The dot operator is used to access members of a struct directly, e.g., person.name.
- A
- 38
What is a drawback of using parallel arrays to store related data?
- A
They require more memory than an array of structs.
- B
They cannot store strings.
- C
They are slower to access than a single array.
- D
It is easy to accidentally misalign the data between arrays.
Correct answer
DIt 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.
- A
- 39
Why are magic numbers (hardcoded sizes) considered bad practice?
- A
They make the code run slower.
- B
They reduce code readability and maintainability.
- C
They prevent the use of arrays.
- D
They cause syntax errors.
Correct answer
BThey 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.
- A
- 40
In C, what does returning 0 from main typically indicate?
- A
The program ran successfully
- B
The program is still running
- C
An error occurred
- D
The program encountered a crash
Correct answer
AThe program ran successfully
Explanation
By convention, returning 0 from main signals that the program executed successfully, while non-zero values indicate errors.
- A
- 41
How can you initialize an array with specific values in C?
- A
Arrays cannot be initialized with specific values
- B
By assigning each element individually after declaration
- C
By using curly braces during declaration, e.g., int arr[] = {1,2,3};
- D
By using an assignment statement with a loop
Correct answer
CBy 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.
- A
- 42
Which header file must be included to use the strcmp function?
- A
<string.h>
- B
<stdlib.h>
- C
<stdio.h>
- D
<cs50.h>
Correct answer
A<string.h>
Explanation
The strcmp function is declared in <string.h>, so that header must be included.
- A
- 43
If an array has n elements, what is the index of the last element?
- A
n−1
- B
n+1
- C
n
- D
n/2
Correct answer
An−1
Explanation
C arrays are zero-indexed, so the last element is at index n−1.
- A
- 44
What does it mean for an array to be 'contiguous' in memory?
- A
The array elements are stored in reverse order.
- B
The array elements are stored next to each other without gaps.
- C
The array elements are stored in separate memory regions.
- D
The array elements are stored in random locations.
Correct answer
BThe 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.
- A

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