Back to course home
0% completed
Vote For New Content
Quiz
Question 1
What is the time complexity of accessing an element by its index in an array?
A
O(1)
B
O(n)
C
O(log n)
D
O(n2)
Question 2
What is the time complexity of searching for an element in a singly linked list?
A
O(1)
B
O(n)
C
O(log n)
D
O(n2)
Question 3
What is the space complexity of the following code that finds duplicates in an array using a HashSet?
import java.util.HashSet;
public class Solution {
public boolean hasDuplicate(int[] arr) {
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
if (set.contains(num)) {
return true;
}
set.add(num);
}
return false;
}
}
A
O(1)
B
O(n)
C
O(logn)
D
O(n2)
Question 4
What is the time complexity of the following code for reversing a linked list?
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode current = head;
while (current != null) {
ListNode nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
return prev;
}
}
A
O(1)
B
O(n2)
C
O(n)
D
O(log n)
Question 5
What is the space complexity of the following code that checks for balanced parentheses using a stack?
import java.util.Stack;
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
} else {
if (stack.isEmpty()) return false;
char top = stack.pop();
if ((c == ')' && top != '(') ||
(c == '}' && top != '{') ||
(c == ']' && top != '[')) {
return false;
}
}
}
return stack.isEmpty();
}
}
A
O(n)
B
O(1)
C
O(log n)
D
O(n2)
Question 6
What is the time complexity of the following code that performs a depth-first traversal on a binary tree?
public void dfs(TreeNode root) {
if (root == null) return;
System.out.println(root.val);
dfs(root.left);
dfs(root.right);
}
A
O(1)
B
O(log n)
C
O(n2)
D
O(n)
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible