KatInterview

Peak Subarray, Lone Value, and Spiral Grid Traversal

Arrays & HashingAuto-graded · 23 test cases
MediumAuto-gradedArrays & Hashingarraysdynamic-programmingsimulation

Last updated

Tests Kadane's algorithm with tie-breaking, frequency-based minimum selection, and counterclockwise skip-step grid simulation in one combined function.

Problem

You will implement a single function that solves three independent sub-problems and returns their answers together in a dictionary.


Sub-problem 1 — Peak Contiguous Subarray

Given an integer array nums, find the contiguous subarray whose elements sum to the largest possible value.

Return [max_sum, start, end] where start and end are zero-based, inclusive indices of the winning subarray.

Tie-breaking rules (apply in order):

  1. If two subarrays share the same sum, prefer the one with fewer elements (shorter length).
  2. If they still tie on length, prefer the one whose start index is smaller (appears earlier).

Your solution must run in O(n) time and use O(1) extra space.

When every element is negative, the answer is the single element with the largest (least-negative) value, at its first occurrence.


Sub-problem 2 — Minimum Singleton Value

Given the same array nums, find the smallest integer that appears exactly once.

  • If several values each appear exactly once, return the smallest among them.
  • If no value appears exactly once, return -1.

Sub-problem 3 — Counterclockwise Skip-Step Grid

You are on an m × n grid. Rows and columns are numbered starting from 1, with (1, 1) at the top-left. You begin at (1, 1).

You move through a repeating direction cycle: right → up → left → down → right → …

On each attempt you jump exactly two cells in the current direction (you skip the intermediate cell, which remains unvisited). A jump is valid only if the landing cell is within the grid boundaries and has not been visited before. The skipped cell's visited status is irrelevant.

When the next jump in the current direction would be invalid (out of bounds or already visited), rotate 90° counterclockwise to the next direction and try again. If all four directions are blocked, stop immediately.

Return [row, col] — the one-based coordinates of the last cell you successfully landed on.


Return value

Return a single dictionary with exactly these keys:

{
    "max_subarray": [max_sum, start_index, end_index],
    "min_unique":   <integer or -1>,
    "last_grid_cell": [row, col]
}

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= m, n <= 100
  • The grid always has at least one cell; (1, 1) is always visited and is the answer when no valid jump exists from the start.
  • nums is used for both sub-problems 1 and 2.

Examples

Example 1

Input: combined_array_grid_challenge([-2, 1, -3, 4, -1, 2, 1, -5, 4], 3, 4)

Output: {'max_subarray': [6, 3, 6], 'min_unique': -5, 'last_grid_cell': [3, 1]}

Example 2

Input: combined_array_grid_challenge([-8, -3, -3, -5], 1, 1)

Output: {'max_subarray': [-3, 1, 1], 'min_unique': -8, 'last_grid_cell': [1, 1]}

Example 3

Input: combined_array_grid_challenge([2, 2, 1, 3, 3, -4], 3, 3)

Output: {'max_subarray': [11, 0, 4], 'min_unique': -4, 'last_grid_cell': [3, 1]}

Hints

Hint 1

For the subarray problem, think about when restarting from the current index strictly improves your candidate versus extending the existing one — the restart condition on zero is what enforces the shortest-length tie-break automatically.

Hint 2

For the singleton minimum, a single pass to build a frequency map followed by a filtered minimum is all you need; watch out for the case where -1 itself is a valid answer (the sentinel is still correct per the spec, but worth noting).

Hint 3

For the grid, every jump moves by exactly 2 in one coordinate, so you can only ever land on cells where both the row and column are odd — you are effectively walking a compressed ceil(m/2) × ceil(n/2) grid with unit steps, which bounds the total number of moves.