KatInterview

Solving Multi-Task Online Assessment Challenges

Arrays & HashingModel answer
MediumModel answerArrays & Hashingarrayssimulationgrid

Last updated

A guide to solving four standard assessment tasks: cumulative running totals, step-by-step array reduction, deterministic matrix block placement, and linear arithmetic series optimization.

You are tasked with implementing four independent algorithmic modules often found in technical screen assessments.


Task 1: Milestone Day Identification

Given an array traffic where traffic[i] denotes the daily visitors to a service on day i (0-indexed), and a target integer threshold, determine the earliest day index i on which the cumulative sum of visitors from day 0 up to day i becomes greater than or equal to threshold.

  • If the total standard visitor traffic never meets or exceeds threshold, return -1.

Task 2: Linear Subtraction Algorithm Simulation

Given an array of non-negative integers values, simulate the operation below and compute the accumulated sum total_reduced:

  1. Set total_reduced = 0.
  2. Locate the first entry in values from left to right that is strictly greater than 0. Call this value x. If no such element exists, terminate and return total_reduced.
  3. Starting from the index where x was found, iterate towards the end of the array:
    • If an element is strictly less than x, stop the rightward scan immediately.
    • Otherwise, subtract x from the element and continue to the next position on the right.
  4. Add x to total_reduced.
  5. Repeat from Step 2.

Return total_reduced after all valid rounds are processed.


Task 3: Priority Tetronimo Grid Placement

You are given a matrix grid of dimensions rows x cols initially filled with empty markers '.'. You are also given an ordered list of block identifiers placement_order containing characters from {A, B, C, D, E}. Each character corresponds to a relative shape defined by offsets relative to its top-left bounding anchor coordinate (0, 0).

For each block in placement_order, find the first available coordinate (r, c) in row-major order (smallest row index r, then smallest column index c) such that placing the block's anchor at (r, c) allows all associated shape cells to fit entirely within the grid bounds and on unoccupied ('.') positions.

Place each block without rotation. Once placed, label those grid positions with the block's character. Assume all inputs guarantee that every block in placement_order has at least one valid fitting position.

Return the final 2D array representation of the grid.


Task 4: Minimum Operations for Uniform Linear Slopes

You are given an array elevations of size N, representing the height of structures along a avenue. In a single modification step, you can increase the height of any individual structure by 1.

A configuration is valid if its heights follow one of two continuous unit-step arithmetic progressions across all indices i ($0 \le i < N$):

  1. Strictly Ascending: $H[i] = k + i$ for some integer $k$.
  2. Strictly Descending: $H[i] = k - i$ for some integer $k$.

Because you can only perform height additions (you cannot decrease any height), find the minimum total number of unit increments required to adjust elevations into either a valid Strictly Ascending or valid Strictly Descending structure sequence.

Hints

Hint 1

For Task 1, iterate through the array once while keeping a running sum.

Hint 2

For Task 2, notice that elements never increase and zero values can be safely bypassed; tracking the current scan start index prevents redundant scanning.

Hint 3

For Task 4, express target heights as functions of a single starting value k. Find the lower bound for k forced by the condition target[i] >= elevations[i].