Rank Local Businesses by Time-Decayed Neighbor Recommendations

Last updated: November 29, 2026

Quick Overview

Design a data structure that records neighbor recommendations (Faves) for local businesses with timestamps and weights, and returns the top k businesses in a neighborhood by an exponentially decayed score at query time.

Nextdoor
Coding & Algorithms
Software Engineer
Nextdoor
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Design a data structure that records neighbor recommendations (Faves) for local businesses with timestamps and weights, and returns the top k businesses in a neighborhood by an exponentially decayed score at query time.

Nextdoor surfaces trusted local businesses through neighbor recommendations, and a recommendation from three years ago should count less than one from last month. The interviewer looks for the trick that makes decay cheap (storing scores against a fixed base time) and for sensible handling of overflow and ties.

What the Interviewer Expects
  • Define the decayed score formula with a half-life
  • Notice that decay multiplies every score by the same factor, so ranking at a fixed time needs no per-item updates
  • Store contributions relative to a base time and rebase to avoid overflow
  • Use a size-k heap for top k selection
  • Break ties deterministically
Key Topics to Cover
Exponential decay
Heap top k
Numerical stability
Hash map aggregation
Local ranking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you shard this when there are millions of businesses across thousands of neighborhoods?
  • How do you stop one household from inflating a business with many recommendations?
  • Would you rank by raw decayed sum or by a Bayesian average that also uses negative signals? Why?
  • How would you include recommendations from nearby neighborhoods with a lower weight?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Operations: add(neighborhood, business, t, weight) and top_k(neighborhood, now, k). Score is s(now)=∑iwi⋅0.5(now−ti)/Hs(now) = \sum_i w_i \cdot 0.5^{(now - t_i)/H} with half-life HH days. Adds are frequent; queries are...

High-Level Design

Key insight: 0.5(now−t)/H=2(t−T0)/H⋅2−(now−T0)/H0.5^{(now - t)/H} = 2^{(t - T_0)/H} \cdot 2^{-(now - T_0)/H}. The second factor is shared by every business, so store raw += w * 2^((t - T0)/H) and multiply by one scale factor at que...


Submit Your Answer
Markdown supported

Related Questions