Mettl Coding Skills 2 ā Questions and Answers
Question 1: What is the time complexity of searching for an element in a balanced binary search tree?
- O(n)
- O(log n) (Correct answer)
- O(1)
- O(n log n)
Correct answer: O(log n)
A balanced BST halves the search space at each node, giving O(log n) time complexity.
Question 2: Which sorting algorithm has the best average-case time complexity?
- Bubble Sort
- Insertion Sort
- Merge Sort (Correct answer)
- Selection Sort
Correct answer: Merge Sort
Merge Sort runs in O(n log n) average-case, outperforming the O(n²) algorithms.
Question 3: In Python, what does the following return: `[x**2 for x in range(4)]`?
- [0, 1, 4, 9] (Correct answer)
- [1, 4, 9, 16]
- [0, 2, 4, 6]
- [1, 2, 3, 4]
Correct answer: [0, 1, 4, 9]
range(4) produces 0,1,2,3 and squaring each yields [0, 1, 4, 9].
Question 4: What does the 'virtual' keyword enable in C++?
- Memory allocation on the heap
- Compile-time method binding
- Runtime polymorphism via dynamic dispatch (Correct answer)
- Static class members
Correct answer: Runtime polymorphism via dynamic dispatch
The 'virtual' keyword enables runtime polymorphism by using a vtable for dynamic dispatch.
Question 5: Which data structure uses LIFO (Last In, First Out) ordering?
- Queue
- Stack (Correct answer)
- Heap
- Deque
Correct answer: Stack
A Stack follows LIFO: the last element pushed is the first one popped.
Question 6: What is the output of `print(type([]))` in Python?
- <class 'tuple'>
- <class 'set'>
- <class 'dict'>
- <class 'list'> (Correct answer)
Correct answer: <class 'list'>
Square brackets `[]` create a list object in Python, so type returns <class 'list'>.
Question 7: In SQL, which clause filters rows AFTER a GROUP BY aggregation?
- WHERE
- HAVING (Correct answer)
- FILTER
- ON
Correct answer: HAVING
HAVING filters grouped results, while WHERE filters rows before grouping.
What is the time complexity of searching for an element in a balanced binary search tree?