CodeSignal Technical Assessment Array and String Manipulation 2 — Questions and Answers
Question 1: Given an array [3, 1, 4, 1, 5, 9, 2, 6], what is the result of rotating it left by 3 positions?
- [1, 5, 9, 2, 6, 3, 1, 4] (Correct answer)
- [9, 2, 6, 3, 1, 4, 1, 5]
- [4, 1, 5, 9, 2, 6, 3, 1]
- [6, 3, 1, 4, 1, 5, 9, 2]
Correct answer: [1, 5, 9, 2, 6, 3, 1, 4]
Left rotation by 3 moves the first 3 elements [3,1,4] to the end, leaving [1,5,9,2,6,3,1,4].
Question 2: Which algorithm finds the maximum subarray sum in O(n) time?
- Kadane's Algorithm (Correct answer)
- Boyer-Moore Algorithm
- KMP Algorithm
- Floyd-Warshall Algorithm
Correct answer: Kadane's Algorithm
Kadane's Algorithm iterates once through the array tracking the current and global maximum subarray sum in O(n) time.
Question 3: What does the following produce for s = 'abcde': s[1:4]?
- 'bcd' (Correct answer)
- 'bcde'
- 'abc'
- 'abcd'
Correct answer: 'bcd'
Python slice s[1:4] extracts characters at indices 1, 2, 3 — giving 'bcd'; the end index is exclusive.
Question 4: What is the time complexity of checking whether two strings are anagrams by sorting both?
- O(n log n) (Correct answer)
- O(n)
- O(n²)
- O(1)
Correct answer: O(n log n)
Sorting each string takes O(n log n), which dominates the O(n) comparison step.
Question 5: An array contains integers 1–n with one duplicate and one missing value. Which approach finds both in O(n) time and O(1) space?
- Use XOR and arithmetic sum/sum-of-squares formulas (Correct answer)
- Sort and scan for adjacent duplicates
- Use a hash set to track seen values
- Nested loops comparing every pair
Correct answer: Use XOR and arithmetic sum/sum-of-squares formulas
XOR paired with arithmetic identities (expected sum and sum-of-squares vs actual) isolates both the duplicate and missing value in two passes without extra space.
Question 6: Given strings s = 'racecar', which single operation verifies it is a palindrome most efficiently?
- Compare s with s[::-1] (Correct answer)
- Reverse s character by character and compare
- Sort s and compare with the original
- Count character frequencies
Correct answer: Compare s with s[::-1]
s[::-1] reverses the string in O(n) and a direct equality check confirms palindrome status in one line.
Question 7: When merging two sorted arrays of sizes m and n into a single sorted array, what is the optimal time complexity?
- O(m + n) (Correct answer)
- O(m · n)
- O((m + n) log(m + n))
- O(max(m, n))
Correct answer: O(m + n)
A two-pointer merge traverses each array exactly once, producing the sorted result in O(m + n) time.
Given an array [3, 1, 4, 1, 5, 9, 2, 6], what is the result of rotating it left by 3 positions?