WorksheetsPython Data Structures and Algorithms Quiz
Total questions: 41
Worksheet time: 21mins
What is the time complexity of appending an element to a list in Python?
O(1)
O(log n)
O(n)
O(n²)
What will be the output of the following code? def mystery_function(arr): return [x**2 for x in arr if x % 2 == 0] print(mystery_function([1, 2, 3, 4, 5]))
[1, 4, 9, 16, 25]
[4, 16]
[2, 4]
None
What is the space complexity of a recursive function?
O(1)
O(n)
O(n log n)
O(log n)
Which data structure follows First In, First Out (FIFO)?
Stack
Queue
Array
Linked List
What is the output of the following code? def find_duplicates(arr): return len(arr) != len(set(arr)) print(find_duplicates([1, 2, 3, 4, 1]))
True
False
[1, 2, 3, 4]
[1]
Which of the following is true for a binary search?
The list must be unsorted
The list must be sorted
The list can be unsorted or sorted
The list can only contain negative integers
What is the space complexity of merging two sorted arrays of size n?
O(1)
O(n)
O(2n)
O(log n)
What will be the output of the following Python code? def reverse_string(s): return ''.join(reversed(s)) print(reverse_string("hello"))
olleh
hello
ehllo
None
What is the time complexity of searching an element in a balanced binary search tree?
O(1)
O(n)
O(log n)
O(n²)
What does the following Python code do? def remove_duplicates(lst): return list(set(lst)) print(remove_duplicates([1, 2, 2, 3, 4, 4, 5]))
Removes duplicates and returns a sorted list
Removes duplicates but does not guarantee order
Returns a list with only unique elements in sorted order
Removes duplicates and maintains order of elements
What is the time complexity of finding the middle element of a linked list using the two-pointer approach?
O(1)
O(log n)
O(n)
O(n²)
What will be the output of the following code? def quicksort(arr): if len(arr) <= 1: return arr pivot = arr[len(arr) // 2] left = [x for x in arr if x < pivot] middle = [x for x in arr if x == pivot] right = [x for x in arr if x > pivot] return quicksort(left) + middle + quicksort(right) print(quicksort([3,6,8,10,1,2,1]))
[3, 6, 8, 10, 1, 2, 1]
[1, 1, 2, 3, 6, 8, 10]
[10, 8, 6, 3, 2, 1, 1]
[1, 1, 2, 3, 6, 8]
Which of the following is true for a queue implemented using an array?
The front of the queue is always at the 0th index
The rear of the queue is always at the last index
Elements are dequeued from the front and enqueued at the rear
Elements are dequeued from the rear and enqueued at the front
What will be the output of the following Python code? def factorial(n): return 1 if n == 0 else n * factorial(n-1) print(factorial(5))
24
120
720
5
What is the time complexity of inserting an element in a max heap?
O(log n)
O(n)
O(1)
O(n log n)
Which sorting algorithm divides the input array into two halves, sorts each half recursively, and merges the sorted halves?
Merge Sort
Quick Sort
Bubble Sort
Selection Sort
What is the space complexity of a depth-first search (DFS) on a graph?
O(n)
O(n + m)
O(1)
O(n²)
What will be the output of the following Python code? def fib(n): a, b = 0, 1 for _ in range(n): a, b = b, a + b return a print(fib(6))
5
8
13
21
Which of the following algorithms has the best average case time complexity for sorting?
Bubble Sort
Quick Sort
Insertion Sort
Selection Sort
What is the output of the following Python code? def binary_search(arr, target): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 print(binary_search([1, 3, 5, 7, 9], 5))
1
2
3
-1
What is the time complexity of accessing an element in an array by its index?
O(1)
O(n)
O(log n)
O(n²)
Which of the following statements is true for a singly linked list?
It has both head and tail pointers
It allows direct access to the middle element
Insertion at the end is O(n)
Deletion at the head is O(n)
is true for a singly linked list?
It has both head and tail pointers
It allows direct access to the middle element
Insertion at the end is O(n)
Deletion at the head is O(n)
What will be the output of the following Python code? def is_palindrome(s): return s == s[::-1] print(is_palindrome("racecar"))
True
False
racecar
Error
What is the average-case time complexity of merge sort?
O(n log n)
O(n²)
O(n)
O(log n)
Which of the following data structures is best suited for implementing a LRU (Least Recently Used) cache?
Queue
Stack
Hash Map + Doubly Linked List
Binary Tree
What is the space complexity of breadth-first search (BFS) on a graph?
O(n)
O(n + m)
O(n²)
O(1)
Which of the following sorting algorithms is the most inefficient for large datasets?
Merge Sort
Bubble Sort
Quick Sort
Heap Sort
What will be the output of the following code? def selection_sort(arr): for i in range(len(arr)): min_idx = i for j in range(i+1, len(arr)): if arr[j] < arr[min_idx]: min_idx = j arr[i], arr[min_idx] = arr[min_idx], arr[i] return arr print(selection_sort([64, 25, 12, 22, 11]))
[64, 25, 22, 12, 11]
[11, 12, 22, 25, 64]
[64, 12, 22, 11, 25]
[11, 22, 25, 64, 12]
What is the time complexity of deleting the last node in a singly linked list?
O(1)
O(log n)
O(n)
O(n log n)
Which of the following operations is most efficient on a doubly linked list compared to a singly linked list?
Insertion at the head
Deletion at the tail
Insertion at the tail
Searching for an element
What is the output of the following Python code? def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))
[64, 34, 25, 12, 22, 11, 90]
[90, 64, 34, 25, 22, 12, 11]
[11, 12, 22, 25, 34, 64, 90]
[11, 22, 25, 12, 34, 64, 90]
Which of the following data structures is best suited for implementing an undo operation in text editors?
Queue
Stack
Hash Map
Graph
What is the time complexity of inserting an element at the beginning of an array?
O(1)
O(n)
O(log n)
O(n²)
What is the output of the following Python code? def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i-1 while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr print(insertion_sort([12, 11, 13, 5, 6]))
[5, 6, 11, 12, 13]
[12, 11, 13, 5, 6]
[6, 5, 11, 12, 13]
[11, 12, 5, 6, 13]
What is the space complexity of an iterative in-order traversal of a binary search tree using a stack?
O(1)
O(n)
O(log n)
O(n²)
Which of the following algorithms is used to find the shortest path in a graph with non-negative weights?
Depth-First Search
Dijkstra's Algorithm
Bellman-Ford Algorithm
Breadth-First Search
What will be the output of the following Python code? def merge_sort(arr): if len(arr) > 1: mid = len(arr) // 2 left = arr[:mid] right = arr[mid:] merge_sort(left) merge_sort(right) i = j = k = 0 while i < len(left) and j < len(right): if left[i] < right[j]: arr[k] = left[i] i += 1 else: arr[k] = right[j] j += 1 k += 1 while i < len(left): arr[k] = left[i] i += 1 k += 1 while j < len(right): arr[k] = right[j] j += 1 k += 1 return arr print(merge_sort([38, 27, 43, 3, 9, 82, 10]))
[38, 27, 43, 3, 9, 82, 10]
[3, 9, 10, 27, 38, 43, 82]
[82, 43, 38, 27, 10, 9, 3]
[27, 38, 43, 9, 10, 3, 82]
Which of the following data structures is best suited for breadth-first search (BFS) on a graph?
Stack
Queue
Hash Map
Binary Tree
What is the time complexity of checking if a number is prime by testing divisibility from 2 to √n?
O(1)
O(n)
O(log n)
O(√n)
What will be the output of the following Python code? def count_digits(n): return len(str(n)) print(count_digits(12345))
5
4
6
None
