This pair of problems evaluates proficiency in randomized algorithms and numerical scalability for weighted sampling and in sparse linear algebra for computing dot products, testing competencies in data structures, algorithmic complexity, and handling large numeric ranges within the Coding & Algorithms domain relevant to machine learning engineering; the required level combines practical implementation detail with conceptual reasoning about numerical stability and performance trade-offs. They are commonly asked in technical interviews to gauge the ability to design time- and memory-efficient solutions for high-dimensional or high-volume data, reason about overflow and sparsity implications, and consider reuse or incremental updates to avoid redundant computation.

You are given a mapping from city → population (all populations are positive integers). Implement a random generator that simulates picking one random person uniformly from the total population and returns the city that person lives in.
Map<String, long> populationByCity
.
CityPicker(populationByCity)
(preprocess)
String pickCity()
(can be called many times)
pickCity()
may be called many times (e.g.,
+)
Compute the dot product of two high-dimensional sparse vectors.
Two sparse vectors A and B, each represented as a list of non-zero entries:
A = [(i1, v1), (i2, v2), ...]
B = [(j1, w1), (j2, w2), ...]
Where indices are integers (e.g., in
[0, D)
) and values are numeric. Indices may be assumed sorted ascending unless you choose otherwise.
Return the dot product:
Suppose you will compute dot products repeatedly and only a small number of entries change between calls (e.g., one vector receives point updates, or you repeatedly query dot products with similar vectors). Describe how you would reuse the previous result to avoid recomputing from scratch.
(Do not provide code; explain the approach and complexity tradeoffs.)