Sample questions
Question 1Easy
What does this Python code print?
a = [1, 2, 3, 4, 5, 6, 7]
k = 3
print(a[-k:] + a[:-k])
- [4, 5, 6, 7, 1, 2, 3]
- [5, 6, 7, 1, 2, 3, 4]
- [1, 2, 3, 4, 5, 6, 7]
- [3, 2, 1, 7, 6, 5, 4]
Answer: B. [5, 6, 7, 1, 2, 3, 4]
Explanation
a[-3:] is [5, 6, 7] and a[:-3] is [1, 2, 3, 4]; concatenating them rotates the list right by 3.
Step-by-step solution
- a[-3:] is the last three elements: [5, 6, 7].
- a[:-3] is everything except the last three: [1, 2, 3, 4].
- Concatenating gives [5, 6, 7, 1, 2, 3, 4], a right rotation by 3.
Why the other options are wrong
- A. That is a left rotation by 3: a[3:] + a[:3].
- C. The slices do change the order.
- D. Nothing is reversed.
Tip: Right rotate by k: a[-k:] + a[:-k].
Question 2Easy
Which approach checks whether a string is a palindrome in O(n) time and O(1) extra space?
- Count character frequencies
- Sort the characters
- Compare characters with two pointers moving inward from both ends
- Reverse it into a new string and compare
Answer: C. Compare characters with two pointers moving inward from both ends
Explanation
Two pointers compare s[i] with s[n-1-i] without building a new string; reversing needs O(n) extra space.
Step-by-step solution
- Put i at the start and j at the end.
- Compare s[i] and s[j]; move inward.
- Any mismatch means not a palindrome. O(n) time, O(1) space.
Why the other options are wrong
- A. Frequency counts can show an anagram of a palindrome exists, not that the string is one.
- B. Sorting loses the order.
- D. Reversing needs O(n) extra space.
Tip: Palindrome check: compare from both ends inward.
Question 3Easy
What is the maximum sum of any 3 consecutive elements of [4, 2, 12, 3, 8, 1, 7, 9]?
- 29
- 23
- 24
- 18
Answer: B. 23
Explanation
Slide a window of size 3: the window sums are 18, 17, 23, 12, 16, 17, so the maximum is 23.
Step-by-step solution
- First window 4 + 2 + 12 = 18.
- Slide: 17, 23, 12, 16, 17.
- Maximum = 23, from 12 + 3 + 8.
Why the other options are wrong
- A. 29 adds 12, 8 and 9, which are not consecutive.
- C. No window sums to 24.
- D. 18 is only the first window.
Tip: New sum = old sum + entering - leaving.
Question 4Easy
What is the time complexity of the expand-around-centre method for the longest palindromic substring?
- O(n^2)
- O(n log n)
- O(n)
- O(n^3)
Answer: A. O(n^2)
Explanation
There are 2n - 1 centres and each expansion can take O(n). Manacher's algorithm achieves O(n).
Step-by-step solution
- There are 2n - 1 centres (characters and gaps).
- Each expansion can take up to O(n).
- Total O(n^2) time and O(1) space.
Why the other options are wrong
- B. No halving is involved.
- C. O(n) needs Manacher's algorithm.
- D. O(n^3) is checking every substring.
Tip: Expand around centre: O(n^2). Manacher: O(n).
Question 5Medium
What is the length of the longest substring of "eceba" that contains at most 2 distinct characters?
- 2
- 4
- 3
- 5
Answer: C. 3
Explanation
A sliding window with a character-count map shrinks whenever it holds more than 2 distinct characters; the longest valid window has length 3.
Step-by-step solution
- e, ec, ece: 2 distinct, length 3.
- Adding b makes 3 distinct, so shrink to eb; then a gives ba.
- No window longer than 3 has at most 2 distinct characters.
Why the other options are wrong
- A. 2 misses ece.
- B. Any 4-length window has 3 distinct characters.
- D. The whole string has 4 distinct characters.
Tip: Shrink while distinct > k.
Question 6Medium
Run the Boyer-Moore majority vote algorithm on [2, 2, 1, 1, 1, 2, 2] (when count is 0, take the current element as candidate with count 1; otherwise add 1 if it matches the candidate, else subtract 1). What are the final candidate and count?
- candidate 2, count 4
- candidate 1, count 1
- candidate 2, count 3
- candidate 2, count 1
Answer: D. candidate 2, count 1
Explanation
Pairing off different elements leaves candidate 2 with count 1. A second pass is needed to confirm it really occurs more than n/2 times.
Step-by-step solution
- 2: cand 2, c 1. 2: c 2. 1: c 1. 1: c 0.
- 1: cand 1, c 1. 2: c 0.
- 2: cand 2, c 1.
- Final: candidate 2, count 1.
Why the other options are wrong
- A. 4 is how many 2s there are, not the vote count.
- B. 1 is the candidate only temporarily.
- C. The count is reduced by the 1s.
Tip: Final count is not the frequency.
Question 7Medium
What is the length of the longest substring of "ccaabbbab" that contains at most 2 distinct characters?
- 9
- 7
- 8
- 6
Answer: B. 7
Explanation
A sliding window with a character-count map shrinks whenever it holds more than 2 distinct characters; the longest valid window has length 7.
Step-by-step solution
- cc then a: {c, a}, length 4 (ccaa).
- b makes 3 distinct: shrink until only a and b remain.
- aabbbab has only a and b: length 7.
Why the other options are wrong
- A. The whole string has 3 distinct characters.
- C. 8 would include a c.
- D. 6 misses one character of aabbbab.
Tip: Shrink while distinct > k.
Question 8Medium
What does this code print?
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
r = [list(row) for row in zip(*m[::-1])]
print(r[0], r[2])
- [1, 4, 7] [3, 6, 9]
- [7, 4, 1] [9, 6, 3]
- [7, 8, 9] [1, 2, 3]
- [3, 6, 9] [1, 4, 7]
Answer: B. [7, 4, 1] [9, 6, 3]
Explanation
Reversing the rows and transposing rotates the matrix 90 degrees clockwise: rows become [7, 4, 1], [8, 5, 2], [9, 6, 3].
Step-by-step solution
- m[::-1] reverses the rows: [7,8,9], [4,5,6], [1,2,3].
- zip(*...) transposes: [7,4,1], [8,5,2], [9,6,3].
- r[0] = [7, 4, 1], r[2] = [9, 6, 3].
Why the other options are wrong
- A. That is the transpose without reversing.
- C. Only reverses the rows.
- D. That is a counter-clockwise rotation.
Question 9Hard
What is the KMP failure (longest proper prefix that is also a suffix) array for the pattern "AAACAAAA"?
- [0, 1, 2, 0, 1, 2, 3, 4]
- [0, 1, 2, 0, 1, 2, 3, 2]
- [0, 1, 2, 0, 1, 2, 3, 3]
- [0, 0, 1, 2, 0, 1, 2, 3]
Answer: C. [0, 1, 2, 0, 1, 2, 3, 3]
Explanation
For each prefix, take the length of the longest proper prefix that is also its suffix: [0, 1, 2, 0, 1, 2, 3, 3].
Step-by-step solution
- A: 0. AA: 1. AAA: 2. AAAC: 0.
- AAACA: 1. AAACAA: 2. AAACAAA: 3.
- AAACAAAA: the next A should match C at index 3; it fails, fall back to 2, and AAA matches: 3.
- Result [0, 1, 2, 0, 1, 2, 3, 3].
Why the other options are wrong
- A. 4 would need AAAC as the suffix.
- B. Falling back from 3 lands at 2, then extends to 3.
- D. AA already gives 1 at index 1.
Tip: On mismatch, fall back to lps[len - 1] and try again.
Question 10Hard
The sliding-window technique finds the length of the longest substring without repeating characters. What is it for "abcabcbb"?
- 3
- 2
- 4
- 8
Answer: A. 3
Explanation
The window expands while characters are unique and shrinks past a repeated character. The longest such substrings are "abc", "bca", "cab", each of length 3.
Step-by-step solution
- Expand the right end while characters are unique.
- abc has length 3; adding a repeats 'a', so move the left end past the first a.
- The window slides through bca, cab, abc, ... and never exceeds 3.
- At 'bb' it shrinks to b, so the answer is 3.
Why the other options are wrong
- B. Longer windows such as abc exist.
- C. Any 4-length window has a repeat, e.g. abca.
- D. The whole string has repeats.
Tip: Remember the last index of each character and jump the left end past it.
Practise all 85 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →