KatInterview

Four Utility Functions: Crop, Softmax, Gaussian, Triangle

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingmathnumerical-methodsgeometry

Last updated

Tests array slicing, numerical stability in exponential calculations, 2D kernel generation, and geometric inequality validation in a single bundled function.

Problem Statement

Implement a function that performs four distinct computational tasks and returns their results in a dictionary. Each task addresses a common pattern in data processing and algorithm implementation.

Task Specifications

Your function receives seven parameters and must return a dictionary with exactly four keys: "center_crop", "softmax", "gaussian_kernel", and "valid_triangle".

Task 1: Center Crop

Extract the central region of a 3D nested list representing an image.

  • Input: image is a nested list with shape (H, W, C) where each pixel location contains a list of channel values. You also receive target dimensions new_h and new_w.
  • Output: Return the center-cropped region of size (new_h, new_w, C) as a nested list.
  • Behavior: Calculate the top-left corner of the crop using integer division: row_offset = (H - new_h) // 2 and col_offset = (W - new_w) // 2. Extract rows from row_offset to row_offset + new_h - 1 (inclusive), and within each row, extract columns from col_offset to col_offset + new_w - 1 (inclusive).

Task 2: Numerically Stable Softmax

Compute the softmax transformation of a 1D list.

  • Input: x is a list of floats.
  • Output: Return a list of the same length where each element is exp(x[i] - max_val) / sum_of_all_exponentials, with max_val = max(x).
  • Behavior: Subtracting the maximum value before exponentiation prevents overflow on large inputs. If x is empty, return an empty list.

Task 3: 2D Gaussian Kernel

Generate a square Gaussian kernel normalized to sum to 1.

  • Input: k (kernel size) and sigma (standard deviation).
  • Output: Return a k × k nested list representing the kernel.
  • Behavior: Define the kernel center at coordinate center = (k - 1) / 2.0. For each position (row, col), compute the value as exp(-distance_squared / (2 * sigma^2)) where distance_squared = (row - center)^2 + (col - center)^2. After computing all values, divide each by the total sum so the kernel sums to exactly 1.

Task 4: Valid Triangle Check

Determine whether three side lengths can form a non-degenerate triangle.

  • Input: sides is a list of floats.
  • Output: Return True if the sides form a valid triangle, otherwise False.
  • Behavior: A valid triangle requires exactly three sides, all strictly positive, where the sum of any two sides is strictly greater than the third. Checking only the smallest two sides against the largest is sufficient: if a ≤ b ≤ c, verify a + b > c.

Constraints

  • 1 ≤ H, W ≤ 1000
  • 1 ≤ new_h ≤ H and 1 ≤ new_w ≤ W
  • 0 ≤ len(x) ≤ 1000
  • -10000 ≤ x[i] ≤ 10000
  • 1 ≤ k ≤ 100
  • 0.1 ≤ sigma ≤ 10
  • 0 ≤ len(sides) ≤ 10
  • All numeric inputs are valid (no NaN or infinity)

Examples

Example 1

Input: compute_four_tasks([[0, 1, 2], [3, 4, 5], [6, 7, 8]], 2, 2, [1000, 1001], 3, 1, [3, 4, 5])

Output: {'center_crop': [[0, 1], [3, 4]], 'softmax': [0.2689414213699951, 0.7310585786300049], 'gaussian_kernel': [[0.07511360795411151, 0.12384140315297397, 0.07511360795411151], [0.12384140315297397, 0.2041799555716581, 0.12384140315297397], [0.07511360795411151, 0.12384140315297397, 0.07511360795411151]], 'valid_triangle': True}

Example 2

Input: compute_four_tasks([[1, 2], [3, 4]], 1, 1, [0, 0], 1, 2, [1, 1, 3])

Output: {'center_crop': [[1]], 'softmax': [0.5, 0.5], 'gaussian_kernel': [[1]], 'valid_triangle': False}

Example 3

Input: compute_four_tasks([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], 3, 2, [-1000, 0, 1000], 5, 0.5, [2, 2, 3])

Output: {'center_crop': [[2, 3], [6, 7], [10, 11]], 'softmax': [0, 0, 1], 'gaussian_kernel': [[6.962478188799074e-8, 0.000028088641754266942, 0.00020754854966504427, 0.000028088641754266942, 6.962478188799074e-8], [0.000028088641754266942, 0.011331766853773574, 0.08373106098253583, 0.011331766853773574, 0.000028088641754266942], [0.00020754854966504427, 0.08373106098253583, 0.6186935068229404, 0.08373106098253583, 0.00020754854966504427], [0.000028088641754266942, 0.011331766853773574, 0.08373106098253583, 0.011331766853773574, 0.000028088641754266942], [6.962478188799074e-8, 0.000028088641754266942, 0.00020754854966504427, 0.000028088641754266942, 6.962478188799074e-8]], 'valid_triangle': True}

Hints

Hint 1

For the crop, use integer division to find the starting indices, handling any odd remainder by placing it on the bottom/right edges.

Hint 2

Softmax stability: subtract max(x) before computing exponentials to prevent overflow while preserving the mathematical result.

Hint 3

The Gaussian kernel center at (k-1)/2 ensures symmetry for both odd and even k; normalize by dividing all entries by their sum.

Hint 4

For triangle validation, sort the three sides first—then you only need to check whether the two smallest sum to more than the largest.