cswork / libraryOpen practice workspace

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.

  1. Implement rotateLeft(nums, k) that returns the array after rotating it left by k positions.
  2. Implement rotateRight(nums, k) that returns the array after rotating it right by k positions.

Notes / Constraints

  • k can be larger than n; treat k as k % n.
  • Handle edge cases like n = 0 or n = 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]

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 number
  • col: the 0-based column number
  • pos: 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:

  • k may be larger than n; treat it as k % n.
  • Handle edge cases: n = 0 returns an empty array, n = 1 returns the array unchanged.
  • A left rotation by k moves the first k elements to the end; a right rotation by k moves the last k elements 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

  1. Reduce k modulo n first so that rotations larger than the array length wrap correctly.
  2. A left rotation by k is a slice split: nums[k:] + nums[:k].
  3. A right rotation by k is equivalent to a left rotation by n - k: nums[n-k:] + nums[:n-k].
  4. 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:

  • row is the 0-based row index,
  • col is the 0-based column index within that row,
  • pos is 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

  1. 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.
  2. Within a cell, repeatedly call find(key, start) and advance start to idx + 1 (not idx + len(key)) so overlapping matches are counted.
  3. Stop the inner loop when find returns -1.
  4. Treat an empty key as a special case and return no matches to avoid an infinite loop.