This question evaluates algorithm design and analysis skills across string processing (efficient substring search) and randomized/data-structure techniques (weighted random sampling with update considerations), testing time/space complexity reasoning and probabilistic thinking for large-scale inputs.
Two coding questions were asked in the onsite.
text
and
pattern
, return the starting index of the first occurrence of
pattern
in
text
, or
-1
if
pattern
does not occur. Assume the input can be large enough that a purely naive
O(n * m)
scan may be too slow.
pickIndex()
that returns index
i
with probability
weights[i] / sum(weights)
. Follow-up: briefly discuss how the design would change if weights were updated frequently.