PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This multi-part Python assessment evaluates image and data processing competencies including array and matrix manipulation (center crop, rotation), numerical stability and probabilistic normalization (softmax), Gaussian kernel construction for blurring, and basic algorithmic/geometry checks (triangle inequality).

  • medium
  • Luma
  • Coding & Algorithms
  • Machine Learning Engineer

Solve Seven Python Image and Data Tasks

Company: Luma

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a Python 3 online assessment with seven independent coding tasks. For each task, implement a function named `solution` with the specified signature. Unless otherwise stated, return a new value and do not rely on unavailable external state. ## 1. Center Crop Implement: ```python def solution(image: list[list[int]], crop_height: int, crop_width: int) -> list[list[int]]: pass ``` Given a grayscale image represented as a 2D list of integers with shape `H x W`, return the centered crop with shape `crop_height x crop_width`. Requirements: - `crop_height <= H` and `crop_width <= W`. - The crop is centered in the original image. - It is guaranteed that `H - crop_height` and `W - crop_width` are even, so the crop removes the same number of rows or columns from both sides. - Do not use NumPy. ## 2. Rotate Image 90 Degrees Clockwise Implement: ```python def solution(image: list[list[int]]) -> list[list[int]]: pass ``` Given a grayscale image represented as a 2D list of integers with shape `H x W`, return a new 2D list with shape `W x H` representing the image rotated 90 degrees clockwise. Requirements: - You may use Python built-ins. - You may use NumPy for basic array manipulation, but you may not use a direct rotation helper such as `rot90`. ## 3. Softmax Implement: ```python import math def solution(z: list[float]) -> list[float]: pass ``` Given a list of logits `z`, return the softmax probabilities. For each index `i`, the output is: ```text exp(z[i]) / sum(exp(z[j]) for all j) ``` Requirements: - Return a list of floats of the same length as `z`. - Do not use NumPy, PyTorch, or similar libraries. - You may use `math.exp`. - The implementation should avoid numerical overflow or instability for large logits. ## 4. Gaussian Blur Kernel Implement: ```python def solution(k: int, sigma: float) -> list[list[float]]: pass ``` Return a 2D Gaussian blur kernel of size `k x k`. For each cell, compute the unnormalized Gaussian weight using: ```text G(x, y) = (1 / (2 * pi * sigma^2)) * exp(-(x^2 + y^2) / (2 * sigma^2)) ``` where `x` and `y` are distances from the center of the kernel. Requirements: - `k` is the kernel size and should be odd for symmetry, for example `3`, `5`, or `7`. - `sigma` is the standard deviation controlling the spread of the Gaussian. - The center cell has coordinate offset `(0, 0)`. - Normalize the kernel so that the sum of all values is `1.0`. - Return the kernel as a 2D list of floats. ## 5. Triangle From Adjacent Elements Implement: ```python def solution(arr: list[int]) -> list[int]: pass ``` You are given an array of positive integers `arr`. For every adjacent triple `(arr[i], arr[i + 1], arr[i + 2])`, determine whether those three values can be side lengths of a triangle. Return an array of length `len(arr) - 2`, where the `i`th element is: - `1` if a triangle can be formed from `arr[i]`, `arr[i + 1]`, and `arr[i + 2]`. - `0` otherwise. A triangle can be formed from side lengths `a`, `b`, and `c` if: ```text a + b > c a + c > b b + c > a ``` Examples: ```text arr = [1, 2, 2, 4] output = [1, 0] ``` ```text arr = [2, 10, 2, 10, 2] output = [0, 1, 0] ``` ```text arr = [1000000000, 1000000000, 1000000000, 1000000000] output = [1, 1] ``` Constraints: - `3 <= len(arr) <= 1000` - `1 <= arr[i] <= 10^9` ## 6. Minimum Euclidean Distance Implement: ```python def solution(p: list[list[int]]) -> float: pass ``` You are given an array `p` of points on a Cartesian plane. Each point is represented as `[x, y]`. Return the minimum Euclidean distance between two points with different indices. The Euclidean distance between points `(x1, y1)` and `(x2, y2)` is: ```text sqrt((x1 - x2)^2 + (y1 - y2)^2) ``` Example: ```text p = [[0, 11], [-7, 1], [-5, -3]] output = 4.472135955 ``` Constraints: - `2 <= len(p) <= 20000` - Each `p[i]` contains exactly two integers. - `abs(p[i][j]) <= 10^7` - Your answer is accepted if its absolute error is at most `10^-5`. ## 7. Impute Missing Values and Normalize Implement: ```python def solution(dataset: list[list[float]]) -> list[list[float]]: pass ``` You are given a matrix `dataset` of positive floating-point values. Missing values are represented by `0`. Perform the following two steps: 1. For each column, replace every `0` with the mean of the non-zero values originally present in that column. 2. After imputation, normalize each column so that its mean is `0` and its population standard deviation is `1`. For every value `e` in column `c`, transform it to: ```text (e - column_mean) / column_std ``` where `column_mean` and `column_std` are computed after missing values have been imputed. Use population standard deviation: ```text sqrt(sum((value - column_mean)^2 for value in column) / number_of_rows) ``` Example: ```text dataset = [ [1, 2, 0], [0, 1, 1], [5, 6, 5] ] ``` After replacing missing values: ```text [ [1, 2, 3.0], [3.0, 1, 1], [5, 6, 5] ] ``` A valid normalized output is approximately: ```text [ [-1.224744871391589, -0.4629100498862757, 0.0], [0.0, -0.9258200997725514, -1.224744871391589], [1.224744871391589, 1.3887301496588271, 1.224744871391589] ] ``` Constraints: - `2 <= number of rows <= 50` - `1 <= number of columns <= 50` - All rows have the same number of columns. - Each column has at least two distinct non-zero values. - Each output value is accepted if its absolute error is at most `10^-5`.

Quick Answer: This multi-part Python assessment evaluates image and data processing competencies including array and matrix manipulation (center crop, rotation), numerical stability and probabilistic normalization (softmax), Gaussian kernel construction for blurring, and basic algorithmic/geometry checks (triangle inequality).

Part 1: Center Crop

Given a grayscale image as a rectangular 2D list of integers, return a new centered crop with the requested height and width. The crop dimensions are guaranteed to fit, and the number of removed rows and columns is even so the crop can be centered exactly.

Constraints

  • 1 <= H, W
  • 1 <= crop_height <= H
  • 1 <= crop_width <= W
  • H - crop_height is even
  • W - crop_width is even
  • Do not use NumPy

Examples

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

Expected Output: [[6, 7], [10, 11]]

Explanation: Removing one row from the top and bottom and one column from the left and right leaves the center 2 x 2 block.

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

Expected Output: [[1, 2, 3], [4, 5, 6]]

Explanation: The requested crop is the full image.

Input: ([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30]], 3, 2)

Expected Output: [[9, 10], [15, 16], [21, 22]]

Explanation: The crop starts at row 1 and column 2.

Input: ([[42]], 1, 1)

Expected Output: [[42]]

Explanation: Single-pixel image edge case.

Hints

  1. Compute the first row and first column of the crop using integer division.
  2. List slicing can copy each selected row segment into the returned image.

Part 2: Rotate Image 90 Degrees Clockwise

Given a grayscale image as a rectangular 2D list of integers with shape H x W, return a new image with shape W x H representing the original image rotated 90 degrees clockwise.

Constraints

  • 1 <= H, W
  • All rows have the same length
  • The input image should not be mutated
  • Direct rotation helpers such as numpy.rot90 are not needed

Examples

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

Expected Output: [[4, 1], [5, 2], [6, 3]]

Explanation: The 2 x 3 image becomes a 3 x 2 image.

Input: ([[1, 2, 3], [4, 5, 6], [7, 8, 9]],)

Expected Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

Explanation: Standard square matrix clockwise rotation.

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

Expected Output: [[1], [2], [3], [4]]

Explanation: A single row becomes a single column.

Input: ([[1], [2], [3]],)

Expected Output: [[3, 2, 1]]

Explanation: A single column becomes a single row.

Hints

  1. In the rotated image, each new row corresponds to one original column.
  2. For a clockwise rotation, read each original column from bottom to top.

Part 3: Softmax

Given a list of logits, return their softmax probabilities. For each element z[i], the probability is exp(z[i]) divided by the sum of exp(z[j]) over all indices j. The implementation must be numerically stable for large logits.

Constraints

  • 0 <= len(z) <= 100000
  • Each z[i] is a finite float
  • Do not use NumPy, PyTorch, or similar libraries
  • Use a numerically stable implementation

Examples

Input: ([1.0, 2.0, 3.0],)

Expected Output: [0.09003057317038046, 0.24472847105479764, 0.6652409557748218]

Explanation: The largest logit receives the highest probability.

Input: ([1000.0, 1000.0],)

Expected Output: [0.5, 0.5]

Explanation: A stable implementation handles large equal logits without overflow.

Input: ([-1.0],)

Expected Output: [1.0]

Explanation: A single element always has probability 1.

Input: ([],)

Expected Output: []

Explanation: Empty input edge case.

Input: ([0.0, 0.0, 0.0],)

Expected Output: [0.3333333333333333, 0.3333333333333333, 0.3333333333333333]

Explanation: Equal logits produce a uniform distribution.

Hints

  1. Subtracting the maximum logit from every logit does not change the final probabilities.
  2. After shifting, exponentials are at most exp(0), which avoids overflow.

Part 4: Gaussian Blur Kernel

Return a normalized 2D Gaussian blur kernel of size k x k. For each cell, use its offset from the center as coordinates x and y, compute the Gaussian weight, then divide every weight by the total sum so the final kernel sums to 1.0.

Constraints

  • 1 <= k <= 101
  • k is odd
  • sigma > 0
  • Return floating-point values
  • The center cell has offset 0, 0

Examples

Input: (1, 1.0)

Expected Output: [[1.0]]

Explanation: A 1 x 1 kernel normalizes to a single value of 1.

Input: (3, 1.0)

Expected Output: [[0.07511360795411151, 0.12384140315297396, 0.07511360795411151], [0.12384140315297396, 0.2041799555716581, 0.12384140315297396], [0.07511360795411151, 0.12384140315297396, 0.07511360795411151]]

Explanation: A symmetric 3 x 3 Gaussian kernel with sigma 1.

Input: (3, 0.5)

Expected Output: [[0.011343736558495071, 0.08381950580221058, 0.011343736558495071], [0.08381950580221058, 0.6193470305571772, 0.08381950580221058], [0.011343736558495071, 0.08381950580221058, 0.011343736558495071]]

Explanation: A smaller sigma concentrates more weight in the center.

Hints

  1. Let center = k // 2. For row r and column c, offsets are y = r - center and x = c - center.
  2. The constant factor in the Gaussian formula is allowed, but normalization will make the final sum equal to 1 either way.

Part 5: Triangle From Adjacent Elements

Given an array of positive integers, examine every adjacent triple. For each triple, return 1 if the three values can be side lengths of a triangle, and 0 otherwise.

Constraints

  • 3 <= len(arr) <= 1000
  • 1 <= arr[i] <= 1000000000
  • A triangle requires a + b > c, a + c > b, and b + c > a

Examples

Input: ([1, 2, 2, 4],)

Expected Output: [1, 0]

Explanation: 1, 2, 2 forms a triangle, but 2, 2, 4 is degenerate.

Input: ([2, 10, 2, 10, 2],)

Expected Output: [0, 1, 0]

Explanation: Only the middle triple 10, 2, 10 can form a triangle.

Input: ([1000000000, 1000000000, 1000000000, 1000000000],)

Expected Output: [1, 1]

Explanation: Large equal side lengths form valid triangles.

Input: ([1, 1, 2],)

Expected Output: [0]

Explanation: Minimum length array with a degenerate triple.

Input: ([3, 4, 5],)

Expected Output: [1]

Explanation: A classic valid triangle.

Hints

  1. For positive side lengths, sorting the three values lets you check only whether the two smaller sides sum to more than the largest.
  2. Be careful with degenerate triples such as 1, 1, 2; equality is not enough.

Part 6: Minimum Euclidean Distance

Given a list of points on a Cartesian plane, return the minimum Euclidean distance between two points with different indices. The input may be large, so an O(n^2) comparison of every pair is not efficient enough for the upper constraint.

Constraints

  • 2 <= len(p) <= 20000
  • Each point has exactly two integer coordinates
  • abs(coordinate) <= 10000000
  • Points may have duplicate coordinates

Examples

Input: ([[0, 11], [-7, 1], [-5, -3]],)

Expected Output: 4.47213595499958

Explanation: The closest pair is [-7, 1] and [-5, -3], with distance sqrt(20).

Input: ([[1, 1], [1, 1], [5, 5]],)

Expected Output: 0.0

Explanation: Duplicate points have distance 0 even though their indices differ.

Input: ([[0, 0], [3, 4]],)

Expected Output: 5.0

Explanation: Minimum-size input with exactly two points.

Input: ([[0, 0], [0, 2], [2, 0], [2, 2]],)

Expected Output: 2.0

Explanation: Several neighboring pairs are tied at distance 2.

Input: ([[-10000000, -10000000], [10000000, 10000000], [-9999999, -10000000]],)

Expected Output: 1.0

Explanation: Boundary coordinate values with a closest horizontal distance of 1.

Hints

  1. Sort points by x-coordinate and use a divide-and-conquer closest-pair algorithm.
  2. When merging two halves, only points close to the dividing line can improve the answer.

Part 7: Impute Missing Values and Normalize

Given a matrix of positive floating-point values where 0 represents a missing value, first replace each 0 by the mean of the non-zero values originally present in that column. Then normalize every column so its post-imputation mean is 0 and its population standard deviation is 1.

Constraints

  • 2 <= number of rows <= 50
  • 1 <= number of columns <= 50
  • All rows have the same number of columns
  • Values are positive floats, except 0 marks missing values
  • Each column has at least two distinct non-zero values
  • Use population standard deviation

Examples

Input: ([[1.0, 2.0, 0.0], [0.0, 1.0, 1.0], [5.0, 6.0, 5.0]],)

Expected Output: [[-1.224744871391589, -0.4629100498862757, 0.0], [0.0, -0.9258200997725514, -1.224744871391589], [1.224744871391589, 1.3887301496588271, 1.224744871391589]]

Explanation: Zeros are replaced by column means, then each column is standardized.

Input: ([[1.0], [3.0]],)

Expected Output: [[-1.0], [1.0]]

Explanation: Minimum rows and one-column edge case with no missing values.

Input: ([[0.0, 2.0], [4.0, 0.0], [6.0, 8.0], [0.0, 10.0]],)

Expected Output: [[0.0, -1.585187847], [-1.414213562373095, 0.0], [1.414213562373095, 0.452910814], [0.0, 1.132277034]]

Explanation: Tests imputation in both columns, including multiple missing values in one column.

Input: ([[1.0, 10.0], [2.0, 20.0], [3.0, 30.0]],)

Expected Output: [[-1.224744871391589, -1.224744871391589], [0.0, 0.0], [1.224744871391589, 1.224744871391589]]

Explanation: No missing values; only column normalization is performed.

Hints

  1. Compute imputation means from the original non-zero values before replacing any zeros.
  2. After imputation, compute each column mean and standard deviation independently.
Last updated: Jun 13, 2026

Related Coding Questions

  • Implement Refactoring Assessment Features - Luma (hard)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.