Data Structures & Algorithms Basics
- 34Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary purpose of Big O notation in computer science?
What is the primary purpose of Big O notation in computer science?
34 questions
- 1
What is the primary purpose of Big O notation in computer science?
- A
To measure the memory usage of a program
- B
To compare the syntax of different programming languages
- C
To describe the performance and scalability of an algorithm as input grows
- D
To describe the exact execution time of an algorithm in milliseconds
Correct answer
CTo describe the performance and scalability of an algorithm as input grows
Explanation
Big O notation describes how the runtime or space requirements of an algorithm grow as the input size increases, helping determine scalability.
- A
- 2
Why is Big O notation important when choosing data structures?
- A
It determines the exact number of lines of code needed
- B
It specifies the programming language to use
- C
It helps assess how costly operations become with large inputs
- D
It ensures the code compiles correctly
Correct answer
CIt helps assess how costly operations become with large inputs
Explanation
Different data structures have different time complexities for operations; Big O helps evaluate which structure performs better for large datasets.
- A
- 3
Which of the following best describes the runtime complexity of a simple loop that iterates over all elements of an array of size n?
- A
O(1)
- B
O(logn)
- C
O(n)
- D
O(n2)
Correct answer
CO(n)
Explanation
A single loop that processes each element once has linear growth ā the number of operations is directly proportional to n.
- A
- 4
If an algorithm has runtime O(n+100), how can it be simplified?
- A
O(n2)
- B
O(100n)
- C
O(n)
- D
O(n+100)
Correct answer
CO(n)
Explanation
Constants are dropped in Big O notation because they do not affect the growth rate as n becomes large; O(n+100) simplifies to O(n).
- A
- 5
What is the runtime complexity of a method that contains two consecutive loops, each iterating over the same input of size n?
- A
O(n)
- B
O(nlogn)
- C
O(2n)
- D
O(n2)
Correct answer
AO(n)
Explanation
Two consecutive loops still represent linear growth; O(2n) simplifies to O(n) because constants are dropped.
- A
- 6
If an algorithm processes two different inputs of sizes n and m with separate loops, what is the simplified runtime complexity?
- A
O(nĆm)
- B
O(n+m)
- C
O(max(n,m))
- D
O(n)
Correct answer
BO(n+m)
Explanation
With two independent inputs, the runtime is O(n+m), which is linear in the total input size.
- A
- 7
What is the runtime complexity of a method with a loop that runs n times and, inside it, another loop that also runs n times?
- A
O(n2)
- B
O(2n)
- C
O(n)
- D
O(nlogn)
Correct answer
AO(n2)
Explanation
Nested loops multiply the iterations: O(nĆn)=O(n2), quadratic time.
- A
- 8
If an algorithm runs in O(n+n2), which term dominates and what is the simplified complexity?
- A
O(n)
- B
O(n2)
- C
O(2n2)
- D
O(n3)
Correct answer
BO(n2)
Explanation
The n2 term grows faster than n, so the lower-order term is dropped, simplifying to O(n2).
- A
- 9
Which searching algorithm runs in O(logn) time on a sorted array?
- A
Linear search
- B
Insertion sort
- C
Bubble sort
- D
Binary search
Correct answer
DBinary search
Explanation
Binary search repeatedly divides the search interval in half, achieving logarithmic time complexity.
- A
- 10
In binary search, how many comparisons are needed at most to find an element in a sorted array of 1 million items?
- A
20
- B
1 million
- C
500,000
- D
19
Correct answer
D19
Explanation
Binary search requires at most log2ā(1,000,000)ā20 comparisons, so 19 or 20; the video states 19.
- A
- 11
How does the runtime of an O(logn) algorithm compare to an O(n) algorithm as input grows?
- A
It remains constant
- B
It grows faster
- C
It grows at the same rate
- D
It grows more slowly
Correct answer
DIt grows more slowly
Explanation
Logarithmic growth slows down over time, making it more scalable than linear growth.
- A
- 12
Which growth rate is considered the opposite of logarithmic growth?
- A
Exponential growth
- B
Quadratic growth
- C
Constant growth
- D
Linear growth
Correct answer
AExponential growth
Explanation
Exponential growth (O(2n)) is the inverse of logarithmic growth ā it increases faster and faster.
- A
- 13
What is the space complexity of a method that declares a single integer variable regardless of input size?
- A
O(1)
- B
O(logn)
- C
O(n2)
- D
O(n)
Correct answer
AO(1)
Explanation
A fixed number of variables uses constant space, O(1), independent of input size.
- A
- 14
When analyzing space complexity, what do we consider?
- A
Only the additional memory allocated by the algorithm
- B
Total memory used including input
- C
The memory used by the operating system
- D
Only the memory used by the input
Correct answer
AOnly the additional memory allocated by the algorithm
Explanation
Space complexity focuses on extra memory needed beyond the input, because the input size is already given.
- A
- 15
How are array elements stored in memory?
- A
In a linked structure with pointers
- B
Contiguously in sequential memory locations
- C
In a binary tree format
- D
Randomly scattered
Correct answer
BContiguously in sequential memory locations
Explanation
Arrays store elements in contiguous memory blocks, allowing direct index-based access.
- A
- 16
What is the runtime complexity of accessing an array element by its index?
- A
O(1)
- B
O(n)
- C
O(n2)
- D
O(logn)
Correct answer
AO(1)
Explanation
Accessing an array element by index is a simple address calculation that runs in constant time.
- A
- 17
What is a major limitation of arrays in Java?
- A
They can only store integers
- B
They cannot be iterated using loops
- C
They have a fixed size that cannot change
- D
They store elements in non-contiguous memory
Correct answer
CThey have a fixed size that cannot change
Explanation
Arrays are static; once created, their size cannot be altered without creating a new array.
- A
- 18
What is the runtime complexity of resizing an array that contains n elements?
- A
O(n)
- B
O(n2)
- C
O(logn)
- D
O(1)
Correct answer
AO(n)
Explanation
Resizing requires allocating a new array and copying all existing elements, which takes linear time.
- A
- 19
What is the worst-case runtime complexity of removing an element from the beginning of an array?
- A
O(1)
- B
O(logn)
- C
O(n2)
- D
O(n)
Correct answer
DO(n)
Explanation
Removing from the start requires shifting all remaining elements left, which is O(n).
- A
- 20
When building a dynamic array class, what field keeps track of the number of items actually stored?
- A
capacity
- B
count
- C
items.length
- D
size
Correct answer
Bcount
Explanation
The
countfield tracks the number of items inserted, whileitems.lengthrepresents the allocated capacity. - A
- 21
In the custom dynamic array implementation, how is the array resized when full?
- A
Increase by 50%
- B
Triple the size
- C
Increase by a fixed number (e.g., 10)
- D
Double the size
Correct answer
DDouble the size
Explanation
The video doubles the array size:
new int[count * 2]. - A
- 22
What is the runtime complexity of the
indexOfmethod in the custom array (worst case)?- A
O(n)
- B
O(n2)
- C
O(1)
- D
O(logn)
Correct answer
AO(n)
Explanation
In the worst case (item at the end or not found), the method traverses all elements, so O(n).
- A
- 23
What does the
removeAtmethod do after shifting elements?- A
Decrements the count
- B
Sets the last element to null
- C
Returns the removed value
- D
Increases the array size
Correct answer
ADecrements the count
Explanation
After shifting, the
countis decremented to reflect the reduced number of items. - A
- 24
What are the two Java classes that implement dynamic arrays, and how do they differ?
- A
ArrayList and LinkedList; both grow by 100%
- B
Vector and ArrayList; Vector grows by 50% and is synchronized, ArrayList grows by 100% and is not synchronized
- C
Vector and ArrayList; Vector grows by 100% and is synchronized, ArrayList grows by 50% and is not synchronized
- D
Vector and LinkedList; Vector is thread-safe, LinkedList is not
Correct answer
CVector and ArrayList; Vector grows by 100% and is synchronized, ArrayList grows by 50% and is not synchronized
Explanation
Vector doubles its size and synchronizes methods; ArrayList grows by 50% and methods are not synchronized.
- A
- 25
Which method converts an ArrayList to a regular array?
- A
asArray()
- B
toList()
- C
toArray()
- D
getArray()
Correct answer
CtoArray()
Explanation
The
toArray()method returns an array containing all elements of the ArrayList. - A
- 26
In a linked list, what does each node contain?
- A
Only a value
- B
A value and an index
- C
A value and a pointer to the previous node
- D
A value and a pointer to the next node
Correct answer
DA value and a pointer to the next node
Explanation
Each node stores a value and a reference (pointer) to the next node in the sequence.
- A
- 27
What are the first and last nodes of a linked list called?
- A
Start and End
- B
First and Last
- C
Front and Back
- D
Head and Tail
Correct answer
BFirst and Last
Explanation
The video uses
firstandlast(equivalent to head and tail) for the first and last nodes. - A
- 28
What is the runtime complexity of inserting a new node at the beginning of a linked list?
- A
O(logn)
- B
O(n2)
- C
O(1)
- D
O(n)
Correct answer
CO(1)
Explanation
Inserting at the beginning only requires creating a node and updating the head pointer ā constant time.
- A
- 29
What is the runtime complexity of inserting a node at the end of a linked list when we have a tail reference?
- A
O(n2)
- B
O(n)
- C
O(1)
- D
O(logn)
Correct answer
CO(1)
Explanation
With a tail reference, adding at the end is O(1) because we directly access the last node.
- A
- 30
What is the worst-case runtime complexity of deleting the last node in a singly linked list?
- A
O(1)
- B
O(n2)
- C
O(logn)
- D
O(n)
Correct answer
DO(n)
Explanation
Deleting the last node requires finding the second-to-last node by traversing from the head, which is O(n).
- A
- 31
Why must we remove the link from the previous head when deleting the first node?
- A
To allow Java's garbage collector to reclaim the removed node
- B
To update the tail pointer
- C
To avoid memory leaks in C++
- D
To maintain the list's sorted order
Correct answer
ATo allow Java's garbage collector to reclaim the removed node
Explanation
Java's garbage collector only removes objects with no references; unlinking ensures the old head can be collected.
- A
- 32
In the custom linked list implementation, what is the purpose of the
getPreviousmethod?- A
To return the last node
- B
To return the first node
- C
To check if the list contains a value
- D
To return the previous node of any given node
Correct answer
DTo return the previous node of any given node
Explanation
getPrevioustraverses the list to find the node immediately before a specified node, used inremoveLast. - A
- 33
What exception is thrown when attempting to remove an element from an empty linked list (as per the custom implementation)?
- A
NoSuchElementException
- B
IndexOutOfBoundsException
- C
IllegalArgumentException
- D
NullPointerException
Correct answer
ANoSuchElementException
Explanation
The
NoSuchElementExceptionis thrown to indicate the operation is invalid on an empty list. - A
- 34
In the custom linked list's
addLastmethod, what happens when the list is empty?- A
Only
firstis set to the new node - B
An exception is thrown
- C
Only
lastis set to the new node - D
Both
firstandlastare set to the new node
Correct answer
DBoth
firstandlastare set to the new nodeExplanation
When the list is empty, the new node becomes both the first and last node.
- A

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