Implement array rotations and CSV keyword search
Company: Warner Bros Discovery
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates array manipulation and string parsing competencies, focusing on rotating arrays via modular index arithmetic and locating substring occurrences within a simple CSV while mapping each match to row, column, and character position.
Rotate an Array Left or Right by k
Constraints
- 0 <= n <= 10^5
- 0 <= k <= 10^9
- direction is either "left" or "right"
- -10^9 <= nums[i] <= 10^9
Examples
Input: ([1, 2, 3, 4, 5], 2, 'left')
Expected Output: [3, 4, 5, 1, 2]
Explanation: Move the first 2 elements [1,2] to the end.
Input: ([1, 2, 3, 4, 5], 2, 'right')
Expected Output: [4, 5, 1, 2, 3]
Explanation: Move the last 2 elements [4,5] to the front.
Input: ([1, 2, 3, 4, 5], 7, 'left')
Expected Output: [3, 4, 5, 1, 2]
Explanation: k > n: 7 % 5 = 2, so this matches a left rotation by 2.
Input: ([1, 2, 3, 4, 5], 7, 'right')
Expected Output: [4, 5, 1, 2, 3]
Explanation: k > n: 7 % 5 = 2, so this matches a right rotation by 2.
Input: ([], 3, 'left')
Expected Output: []
Explanation: Empty array stays empty regardless of k.
Input: ([42], 5, 'right')
Expected Output: [42]
Explanation: Single element: k % 1 = 0, array unchanged.
Input: ([1, 2, 3, 4, 5], 5, 'left')
Expected Output: [1, 2, 3, 4, 5]
Explanation: k % n = 0: a full rotation returns the original order.
Input: ([-1, -2, 0, 3], 1, 'right')
Expected Output: [3, -1, -2, 0]
Explanation: Negatives handled like any value; last element moves to front.
Hints
- Reduce k modulo n first so that rotations larger than the array length wrap correctly.
- A left rotation by k is a slice split: nums[k:] + nums[:k].
- A right rotation by k is equivalent to a left rotation by n - k: nums[n-k:] + nums[:n-k].
- Guard the n == 0 case before computing k % n to avoid a division-by-zero.
Find Keyword Occurrences in Simple CSV
Constraints
- 0 <= len(csvText) <= 10^5
- 0 <= len(key) <= 10^3
- csvText uses '\n' as the row separator and ',' as the column separator
- Fields contain no quoted strings or escaped commas
- Matches may overlap
Examples
Input: ("foo,bar\nxxfood,barfoo", "foo")
Expected Output: [[0, 0, 0], [1, 0, 2], [1, 1, 3]]
Explanation: "foo" at row 0 col 0 pos 0; inside "xxfood" at row 1 col 0 pos 2; inside "barfoo" at row 1 col 1 pos 3.
Input: ("abc,abcabc", "abc")
Expected Output: [[0, 0, 0], [0, 1, 0], [0, 1, 3]]
Explanation: Cell 0 has one match; cell 1 ("abcabc") has two non-overlapping matches at pos 0 and 3.
Input: ("hello,world", "xyz")
Expected Output: []
Explanation: Keyword not present anywhere.
Input: ("", "foo")
Expected Output: []
Explanation: The only cell is the empty string, which contains no match.
Input: ("aaa", "aa")
Expected Output: [[0, 0, 0], [0, 0, 1]]
Explanation: Overlapping matches: "aa" begins at pos 0 and pos 1 within "aaa".
Input: ("a,b,c\nd,e,f", "e")
Expected Output: [[1, 1, 0]]
Explanation: "e" appears only in row 1, column 1, at pos 0.
Input: ("foo,bar", "")
Expected Output: []
Explanation: Empty key returns no matches by definition.
Input: ("x,,xx", "x")
Expected Output: [[0, 0, 0], [0, 2, 0], [0, 2, 1]]
Explanation: Empty middle cell yields nothing; "xx" yields two matches at pos 0 and 1.
Hints
- Split csvText on '\n' to get rows, then split each row on ',' to get cells; the indices of these splits are your row and col.
- Within a cell, repeatedly call find(key, start) and advance start to idx + 1 (not idx + len(key)) so overlapping matches are counted.
- Stop the inner loop when find returns -1.
- Treat an empty key as a special case and return no matches to avoid an infinite loop.