Coding questions · Warner Bros Discovery
Implement array rotations and CSV keyword search
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.
Problem A — Rotate an array left/right by k
You are given an integer array nums of length n and an integer k.
- Implement
rotateLeft(nums, k)that returns the array after rotating it left bykpositions. - Implement
rotateRight(nums, k)that returns the array after rotating it right bykpositions.
Notes / Constraints
kcan be larger thann; treatkask % n.- Handle edge cases like
n = 0orn = 1. - Clarify in your solution whether you modify in-place or return a new array.
Example
nums = [1,2,3,4,5],k = 2- left rotate →
[3,4,5,1,2] - right rotate →
[4,5,1,2,3]
- left rotate →
Problem B — Find keyword occurrences in a CSV text
You are given a CSV document as a single string csvText.
- Rows are separated by newline characters
\n. - Columns are separated by commas
,. - Assume fields do not contain escaped commas or quoted strings (i.e., simple CSV).
Task
Given csvText and a keyword key, return all occurrences of key in the document.
For each occurrence, return:
row: the 0-based row numbercol: the 0-based column numberpos: the 0-based character index within that cell where the match begins
Return results in row-major order.
Variant
Extend the function to accept multiple keywords keys[] and return occurrences for any of them (you may also include which keyword matched).
Example
csvText = "foo,bar\nxxfood,barfoo", key = "foo"
- Row 0, Col 0, Pos 0 ("foo")
- Row 1, Col 0, Pos 2 ("xxfood")
- Row 1, Col 1, Pos 3 ("barfoo")
(Exact output format is up to you; it just needs to include row/col/pos.)
Rotate an Array Left or Right by k
You are given an integer array nums of length n, an integer k, and a string direction that is either "left" or "right". Return a NEW array that is nums rotated by k positions in the given direction.
Rules:
kmay be larger thann; treat it ask % n.- Handle edge cases:
n = 0returns an empty array,n = 1returns the array unchanged. - A left rotation by
kmoves the firstkelements to the end; a right rotation bykmoves the lastkelements to the front.
Example: nums = [1,2,3,4,5], k = 2
- direction
"left"->[3,4,5,1,2] - direction
"right"->[4,5,1,2,3]
Constraints
- 0 <= n <= 10^5
- 0 <= k <= 10^9
- direction is either "left" or "right"
- -10^9 <= nums[i] <= 10^9
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
You are given a CSV document as a single string csvText and a keyword key.
- Rows are separated by newline characters
\n. - Columns are separated by commas
,. - Fields are simple: no quoted strings and no escaped commas.
Return every occurrence of key as a triple [row, col, pos], where:
rowis the 0-based row index,colis the 0-based column index within that row,posis the 0-based character index inside that cell where the match begins.
Matches may overlap (e.g. key = "aa" in "aaa" matches at positions 0 and 1). Return results in row-major order (rows in order, then columns in order, then by position within a cell). If key is empty, return an empty list.
Example: csvText = "foo,bar\nxxfood,barfoo", key = "foo"
-> [[0,0,0], [1,0,2], [1,1,3]]
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
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.