Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Python Data Structures and Algorithms Quiz

Total questions: 41

Worksheet time: 21mins

Name
Class
Date
1.

What is the time complexity of appending an element to a list in Python?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n²)

2.

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]))

a)

[1, 4, 9, 16, 25]

b)

[4, 16]

c)

[2, 4]

d)

None

3.

What is the space complexity of a recursive function?

a)

O(1)

b)

O(n)

c)

O(n log n)

d)

O(log n)

4.

Which data structure follows First In, First Out (FIFO)?

a)

Stack

b)

Queue

c)

Array

d)

Linked List

5.

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]))

a)

True

b)

False

c)

[1, 2, 3, 4]

d)

[1]

6.

Which of the following is true for a binary search?

a)

The list must be unsorted

b)

The list must be sorted

c)

The list can be unsorted or sorted

d)

The list can only contain negative integers

7.

What is the space complexity of merging two sorted arrays of size n?

a)

O(1)

b)

O(n)

c)

O(2n)

d)

O(log n)

8.

What will be the output of the following Python code? def reverse_string(s): return ''.join(reversed(s)) print(reverse_string("hello"))

a)

olleh

b)

hello

c)

ehllo

d)

None

9.

What is the time complexity of searching an element in a balanced binary search tree?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n²)

10.

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]))

a)

Removes duplicates and returns a sorted list

b)

Removes duplicates but does not guarantee order

c)

Returns a list with only unique elements in sorted order

d)

Removes duplicates and maintains order of elements

11.

What is the time complexity of finding the middle element of a linked list using the two-pointer approach?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n²)

12.

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]))

a)

[3, 6, 8, 10, 1, 2, 1]

b)

[1, 1, 2, 3, 6, 8, 10]

c)

[10, 8, 6, 3, 2, 1, 1]

d)

[1, 1, 2, 3, 6, 8]

13.

Which of the following is true for a queue implemented using an array?

a)

The front of the queue is always at the 0th index

b)

The rear of the queue is always at the last index

c)

Elements are dequeued from the front and enqueued at the rear

d)

Elements are dequeued from the rear and enqueued at the front

14.

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))

a)

24

b)

120

c)

720

d)

5

15.

What is the time complexity of inserting an element in a max heap?

a)

O(log n)

b)

O(n)

c)

O(1)

d)

O(n log n)

16.

Which sorting algorithm divides the input array into two halves, sorts each half recursively, and merges the sorted halves?

a)

Merge Sort

b)

Quick Sort

c)

Bubble Sort

d)

Selection Sort

17.

What is the space complexity of a depth-first search (DFS) on a graph?

a)

O(n)

b)

O(n + m)

c)

O(1)

d)

O(n²)

18.

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))

a)

5

b)

8

c)

13

d)

21

19.

Which of the following algorithms has the best average case time complexity for sorting?

a)

Bubble Sort

b)

Quick Sort

c)

Insertion Sort

d)

Selection Sort

20.

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))

a)

1

b)

2

c)

3

d)

-1

21.

What is the time complexity of accessing an element in an array by its index?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n²)

22.

Which of the following statements is true for a singly linked list?

a)

It has both head and tail pointers

b)

It allows direct access to the middle element

c)

Insertion at the end is O(n)

d)

Deletion at the head is O(n)

23.

is true for a singly linked list?

a)

It has both head and tail pointers

b)

It allows direct access to the middle element

c)

Insertion at the end is O(n)

d)

Deletion at the head is O(n)

24.

What will be the output of the following Python code? def is_palindrome(s): return s == s[::-1] print(is_palindrome("racecar"))

a)

True

b)

False

c)

racecar

d)

Error

25.

What is the average-case time complexity of merge sort?

a)

O(n log n)

b)

O(n²)

c)

O(n)

d)

O(log n)

26.

Which of the following data structures is best suited for implementing a LRU (Least Recently Used) cache?

a)

Queue

b)

Stack

c)

Hash Map + Doubly Linked List

d)

Binary Tree

27.

What is the space complexity of breadth-first search (BFS) on a graph?

a)

O(n)

b)

O(n + m)

c)

O(n²)

d)

O(1)

28.

Which of the following sorting algorithms is the most inefficient for large datasets?

a)

Merge Sort

b)

Bubble Sort

c)

Quick Sort

d)

Heap Sort

29.

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]))

a)

[64, 25, 22, 12, 11]

b)

[11, 12, 22, 25, 64]

c)

[64, 12, 22, 11, 25]

d)

[11, 22, 25, 64, 12]

30.

What is the time complexity of deleting the last node in a singly linked list?

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n log n)

31.

Which of the following operations is most efficient on a doubly linked list compared to a singly linked list?

a)

Insertion at the head

b)

Deletion at the tail

c)

Insertion at the tail

d)

Searching for an element

32.

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]))

a)

[64, 34, 25, 12, 22, 11, 90]

b)

[90, 64, 34, 25, 22, 12, 11]

c)

[11, 12, 22, 25, 34, 64, 90]

d)

[11, 22, 25, 12, 34, 64, 90]

33.

Which of the following data structures is best suited for implementing an undo operation in text editors?

a)

Queue

b)

Stack

c)

Hash Map

d)

Graph

34.

What is the time complexity of inserting an element at the beginning of an array?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n²)

35.

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]))

a)

[5, 6, 11, 12, 13]

b)

[12, 11, 13, 5, 6]

c)

[6, 5, 11, 12, 13]

d)

[11, 12, 5, 6, 13]

36.

What is the space complexity of an iterative in-order traversal of a binary search tree using a stack?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(n²)

37.

Which of the following algorithms is used to find the shortest path in a graph with non-negative weights?

a)

Depth-First Search

b)

Dijkstra's Algorithm

c)

Bellman-Ford Algorithm

d)

Breadth-First Search

38.

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]))

a)

[38, 27, 43, 3, 9, 82, 10]

b)

[3, 9, 10, 27, 38, 43, 82]

c)

[82, 43, 38, 27, 10, 9, 3]

d)

[27, 38, 43, 9, 10, 3, 82]

39.

Which of the following data structures is best suited for breadth-first search (BFS) on a graph?

a)

Stack

b)

Queue

c)

Hash Map

d)

Binary Tree

40.

What is the time complexity of checking if a number is prime by testing divisibility from 2 to √n?

a)

O(1)

b)

O(n)

c)

O(log n)

d)

O(√n)

41.

What will be the output of the following Python code? def count_digits(n): return len(str(n)) print(count_digits(12345))

a)

5

b)

4

c)

6

d)

None