CodeSignal Technical Assessment Array and String Manipulation 3 — Questions and Answers
Question 1: What is the output of [x**2 for x in range(5) if x % 2 == 0]?
- [0, 4, 16] (Correct answer)
- [0, 1, 4, 9, 16]
- [4, 16]
- [1, 9, 25]
Correct answer: [0, 4, 16]
Even values in range(5) are 0, 2, 4; squaring them yields [0, 4, 16].
Question 2: Which data structure most efficiently supports O(1) duplicate detection while building a result array from an input array?
- Hash set (Correct answer)
- Sorted array
- Min-heap
- Stack
Correct answer: Hash set
A hash set provides O(1) average-case membership checks, making it ideal for tracking seen elements during a single pass.
Question 3: Given array [5, 3, 8, 1, 9, 2, 7], how many comparisons does binary search make to find 7 (assuming the array is first sorted)?
- 3 (Correct answer)
- 7
- 4
- 2
Correct answer: 3
Sorted array is [1,2,3,5,7,8,9]; binary search checks index 3 (5), then index 5 (8), then index 4 (7) — 3 comparisons.
Question 4: What does ''.join(reversed('hello')) return?
- 'olleh' (Correct answer)
- 'hello'
- ['o','l','l','e','h']
- None
Correct answer: 'olleh'
reversed() yields characters in reverse order, and ''.join() concatenates them into the string 'olleh'.
Question 5: What is the space complexity of an in-place array reversal algorithm?
- O(1) (Correct answer)
- O(n)
- O(log n)
- O(n²)
Correct answer: O(1)
In-place reversal swaps elements using a constant number of temporary variables regardless of array size.
Question 6: For the string 'aabcccdddd', what is the run-length encoding?
- 'a2b1c3d4' (Correct answer)
- '2a1b3c4d'
- 'aabcccdddd'
- 'a2bc3d4'
Correct answer: 'a2b1c3d4'
Run-length encoding records each character followed by its count: 'a'×2, 'b'×1, 'c'×3, 'd'×4 → 'a2b1c3d4'.
Question 7: Which approach finds all pairs in an array that sum to a target value in O(n) time?
- Store seen values in a hash map and check target − current element (Correct answer)
- Sort the array and use two pointers
- Use nested loops comparing every pair
- Sort and apply binary search for each element
Correct answer: Store seen values in a hash map and check target − current element
A hash map lookup for target − arr[i] achieves O(1) per element, giving O(n) overall for one pass.
What is the output of [x**2 for x in range(5) if x % 2 == 0]?