Showing 11–20 of 654 questions
What is the correct output of: print(2 ** 3 ** 2) in Python?
EXPLANATION
Exponentiation is right-associative in Python. 32 = 9, then 29 = 512
What is the space complexity of DFS using recursion?
A
O(1)
B
O(n)
C
O(log n)
D
O(h) where h is height
Correct Answer:
D. O(h) where h is height
EXPLANATION
Recursive DFS uses call stack space proportional to the height of the tree/graph. For balanced tree, it's O(log n); worst case O(n).
What is the time complexity of finding the median in a sorted array?
A
O(1)
B
O(log n)
C
O(n)
D
O(n log n)
EXPLANATION
In a sorted array, median is at index n/2, which is accessed in constant time O(1).
In dynamic programming, what is memoization?
A
Breaking problem into subproblems
B
Storing results of subproblems to avoid recomputation
C
Creating a memo
D
Sorting results
Correct Answer:
B. Storing results of subproblems to avoid recomputation
EXPLANATION
Memoization is an optimization technique where results of expensive function calls are cached and returned when same inputs occur again.
What is the result of 1 XOR 1?
EXPLANATION
XOR (exclusive OR) returns 1 only when bits are different. 1 XOR 1 = 0 because both bits are the same.
Which sorting algorithm is stable and has O(n log n) worst-case complexity?
A
QuickSort
B
MergeSort
C
HeapSort
D
Selection Sort
Correct Answer:
B. MergeSort
EXPLANATION
MergeSort has O(n log n) worst-case and is stable. QuickSort is O(n²) worst-case, HeapSort is not stable.
What is the correct way to check if a number is a power of 2?
A
n & (n-1) == 0
B
n % 2 == 0
C
n / 2 == 1
D
sqrt(n) is integer
Correct Answer:
A. n & (n-1) == 0
EXPLANATION
A power of 2 has only one bit set. n & (n-1) removes the rightmost set bit, so if result is 0, n is a power of 2.
In a BFS (Breadth-First Search), what data structure is used?
A
Stack
B
Queue
C
Priority Queue
D
Deque
EXPLANATION
BFS uses a Queue (FIFO) to explore nodes level by level. DFS uses a Stack (LIFO).
What is the space complexity of QuickSort?
A
O(1)
B
O(log n)
C
O(n)
D
O(n log n)
Correct Answer:
B. O(log n)
EXPLANATION
QuickSort uses O(log n) space for the recursion call stack in the average case.
In a hash table with 10 slots, if 15 elements are inserted with a good hash function, what is the expected average chain length?
EXPLANATION
Load factor = n/m = 15/10 = 1.5. Average chain length in hash table = load factor = 1.5