Mettl Coding Fundamentals and Logic 2 — Questions and Answers
Question 1: What is the time complexity of binary search on a sorted array of n elements?
- O(n)
- O(log n) (Correct answer)
- O(n log n)
- O(1)
Correct answer: O(log n)
Binary search halves the search space each iteration, yielding O(log n) time complexity.
Question 2: Which data structure uses LIFO (Last In, First Out) ordering?
- Queue
- Stack (Correct answer)
- Linked List
- Binary Tree
Correct answer: Stack
A stack follows LIFO ordering, where the last element pushed is the first to be popped.
Question 3: What does the following pseudocode return for input n=5? function f(n): if n <= 1: return n return f(n-1) + f(n-2)
- 5
- 8 (Correct answer)
- 3
- 10
Correct answer: 8
This is the Fibonacci sequence; f(5) = f(4)+f(3) = 3+5 = 8.
Question 4: Which sorting algorithm has an average-case time complexity of O(n log n)?
- Bubble Sort
- Insertion Sort
- Merge Sort (Correct answer)
- Selection Sort
Correct answer: Merge Sort
Merge Sort consistently achieves O(n log n) average and worst-case time complexity.
Question 5: In a singly linked list, what is the time complexity of accessing the kth element?
- O(1)
- O(log n)
- O(k) (Correct answer)
- O(n²)
Correct answer: O(k)
You must traverse from the head node, taking O(k) steps to reach the kth element.
Question 6: What value does x hold after this code executes? x = 10 x += 5 x *= 2 x -= 3
- 27 (Correct answer)
- 30
- 22
- 17
Correct answer: 27
10+5=15, 15×2=30, 30−3=27, so x=27.
Question 7: Which of the following correctly describes a hash table collision?
- Two keys map to the same index (Correct answer)
- A key is not found in the table
- The table exceeds its capacity
- Two values are identical
Correct answer: Two keys map to the same index
A collision occurs when two distinct keys produce the same hash index.
What is the time complexity of binary search on a sorted array of n elements?