cswork / libraryOpen practice workspace

Coding questions · Airbnb

Review Python geodata code

This question evaluates proficiency in Python code review and geolocation data-processing, including competencies in readability, correctness, performance optimization, and architectural scalability.

Question

You are given a Python codebase that performs geolocation data processing. Conduct a thorough code review: identify readability, correctness, performance, and scalability issues, and propose concrete refactors or optimizations (e.g., data structures, vectorization, caching, modularization).

Count Distinct Geo Grid Cells

Given a list of GPS points as (latitude, longitude) and an integer precision p, partition the plane into a uniform grid where each cell spans 10^-p degrees in both latitude and longitude. Map each point to its grid cell using integer tile indices i = floor(lat * 10^p + 1e-12) and j = floor(lon * 10^p + 1e-12). Return the number of distinct grid cells visited by the points. If the list is empty, return 0.

Constraints

  • 0 <= n <= 200000
  • -90.0 <= latitude <= 90.0
  • -180.0 <= longitude <= 180.0
  • 0 <= precision <= 6
  • Quantization uses i = floor(lat * 10^precision + 1e-12), j = floor(lon * 10^precision + 1e-12)
  • Return the count of unique (i, j) pairs

Examples

Example 1

Input: points = [[0.123456, 0.987654], [0.123499, 0.987600], [0.123400, 0.987699]], precision = 3
Output: 1

Explanation: Scale by 1000 and floor with small epsilon: all map to (123, 987), so 1 distinct cell.

Example 2

Input: points = [[-0.0001, -0.0001], [0.0, 0.0], [0.0001, 0.0001]], precision = 3
Output: 2

Explanation: Cells are (-1, -1) and (0, 0); thus 2 distinct cells.

Hints

  1. Convert each point to integer tile indices and use a set to track uniqueness.
  2. Precompute scale = 10**precision and avoid Python's round; use math.floor with a tiny epsilon to counter floating-point noise.
  3. If many identical points appear, caching their tile result can reduce repeated computation.

Approach

We quantize each point to an integer grid by scaling by 10^p and flooring with a tiny epsilon to neutralize floating-point artifacts near integers. Each cell is represented as a pair of integers (i, j). Inserting these pairs into a set yields the number of distinct cells. Using integers for cells is cache- and hash-friendly, and avoids ambiguity in floating-point comparisons.

  • Time complexity: O(n)
  • Space complexity: O(n)