KatInterview

Circle Point Generation, Strict Monotonic Run, and Histogram Interpolation

Math & Bit ManipulationModel answer
EasyModel answerMath & Bit Manipulationsamplingarraysinterpolation

Last updated

Solve three foundational tasks: uniform random sampling within a disk, tracking the longest strictly increasing contiguous array, and computing bucketed percentiles.

This technical assessment consists of three distinct computational tasks covering probability, linear array processing, and numerical interpolation.


Task 1: Disk Point Generation

Implement a function generate_disk_point(radius) that generates a coordinate point $(x, y)$ uniformly distributed across the interior (and boundary) of a circle centered at $(0, 0)$ with a given positive radius $R$.


Task 2: Longest Monotonic Ascending Sequence

Implement a function longest_ascending_run(sequence) that finds the length of the longest contiguous sequence of elements that is strictly increasing (i.e., each element must be strictly greater than its predecessor).

Example:

  • For sequence = [1, 3, 5, 4, 7], the function returns 3 (corresponding to [1, 3, 5]).

Task 3: Bucket-Based Quantile Approximation

You are given a list of non-overlapping, sorted continuous histogram bins defined as tuples: (min_val, max_val, count). Assuming that values within each bin are uniformly distributed across its range, implement a function approximate_percentile(bins, percentile) that returns the estimated numerical value at the specified percentile ($0 \le p \le 100$).

Example:

  • Bins: [(0, 10, 50), (10, 20, 50)]
  • percentile = 75
  • Output: 15.0
  • Reasoning: The overall sample count is 100. The target rank is $100 \times \frac{75}{100} = 75$. The first bin accounts for 50 samples. The second bin holds 50 samples between 10 and 20. We require 25 additional samples in the second bin, yielding $10 + (20 - 10) \times \frac{25}{50} = 15.0$.

Constraints

  • $R > 0$
  • $0 \le \text{len}(sequence) \le 10^5$
  • bins is ordered by min_val, covering contiguous intervals without gaps or overlaps.
  • $0 \le percentile \le 100$

Hints

Hint 1

For uniform sampling over a 2D surface area, recall that area scales quadratically with distance from the origin. Linear sampling of radii leads to central clustering.

Hint 2

In Task 2, strictly increasing means sequence[i] > sequence[i-1]. Equal elements reset the current run length back to 1.

Hint 3

For Task 3, remember to filter out or safely skip bins with a count of 0 to avoid division by zero errors during linear interpolation.