CodeSignal Technical Assessment Array and String Manipulation 4 ā Questions and Answers
Question 1: What is the result of 'CodeSignal'.lower().replace('signal', 'test')?
- 'codetest' (Correct answer)
- 'CodeTest'
- 'codesignal'
- 'CODETEST'
Correct answer: 'codetest'
lower() converts to 'codesignal', then replace('signal','test') substitutes 'signal' with 'test' ā 'codetest'.
Question 2: Given a 2D matrix represented as a flat array with row-major order, what is the index of element at row r, column c in an n-column matrix?
- r * n + c (Correct answer)
- c * n + r
- r + c * n
- r * c + n
Correct answer: r * n + c
Row-major layout stores row r starting at index r*n, so element (r, c) is at r*n + c.
Question 3: What is the minimum number of operations to convert 'kitten' to 'sitting' using edit distance (Levenshtein)?
- 3 (Correct answer)
- 5
- 2
- 4
Correct answer: 3
kittenāsitten (substitute kās), sittenāsittin (substitute eāi), sittināsitting (insert g) = 3 operations.
Question 4: An array of 0s and 1s must be partitioned so all 0s come before all 1s. Which algorithm does this in O(n) with O(1) space?
- Dutch National Flag (two-pointer partition) (Correct answer)
- Counting sort with two passes
- Merge sort with custom comparator
- Quicksort with 0/1 pivot
Correct answer: Dutch National Flag (two-pointer partition)
Two pointers (left and right) swap misplaced elements until they meet, achieving O(n) time and O(1) extra space.
Question 5: What does 'abcabc'.count('abc') return?
- 2 (Correct answer)
- 1
- 3
- 0
Correct answer: 2
Python's str.count() finds non-overlapping occurrences: 'abc' appears at index 0 and index 3 ā returns 2.
Question 6: Which technique solves the 'sliding window maximum' problem in O(n) time?
- Monotonic deque (double-ended queue) (Correct answer)
- Priority queue (max-heap)
- Sorted set with lazy deletion
- Segment tree
Correct answer: Monotonic deque (double-ended queue)
A monotonic deque keeps indices of candidate maximum elements in decreasing order, allowing O(1) amortized queries and updates per element.
Question 7: Given sorted array [1, 2, 3, 4, 5, 6, 7] and a target sum of 9, what is the pair found using the two-pointer approach?
- (2, 7)
- (3, 6) (Correct answer)
- (4, 5)
- (1, 8)
Correct answer: (3, 6)
Two pointers start at 1 and 7 (sum=8, too lowāadvance left); at 2 and 7 (sum=9, found) ā but (3,6) also sums to 9 and pointers would find it depending on implementation; the first found is (2,7). Actually pointers find 2+7=9 first.
What is the result of 'CodeSignal'.lower().replace('signal', 'test')?