Listing index: O(1) id lookup with filtered ranking

seniorjava · AI-on
  1. 1Restate the problem in one line
  2. 2Sketch the data model yourself
  3. 3Ask AI for a skeleton ONLY
  4. 4Ask AI for a test plan ONLY
  5. 5Implement the core logic yourself
  6. 6Run the tests — verify, don't trust
  7. 7Ask AI for edge cases you missed
  8. 8Ask AI to review your code
  9. 9Make the final judgment yourself

Problem

Build an in-memory index over home Listing records (id, city, price, beds). getById(id) returns the matching Listing in O(1), or null if absent. search(city, maxPrice) returns the ids of listings in that city priced at or below maxPrice, cheapest first, ties broken by id ascending. Re-adding the same id must update the existing record, not duplicate it.

iThe deal with the pair partner

The AI is deliberately fallible — it gives hints, not solutions, and sometimes proposes wrong code. It cannot touch your editor. Paste what you choose, then Run. Anything AI-derived stays UNVERIFIED until your tests go green.

Self-check

  • getById is a hash lookup (O(1)), not a scan of all listings
  • search filters by exact city and an inclusive price ceiling
  • results are ordered by price ascending, with id ascending as the tie-break
  • re-adding an existing id updates the record in place (no duplicates)
  • you named the time/space cost of search and what you would add for scale (a sorted or per-city index)
Hints / what this doesn't test

This is an Opendoor-flavored applied problem: model records and serve two access patterns — a point lookup and a filtered, ranked query. Restate it first — "So getById has to be O(1), and search is a city + price-ceiling filter sorted by price then id, and adding a known id is an upsert?"

What this problem does not test (say so out loud): pagination, multi-field filters (beds, geo radius), full-text matching, and a precomputed sorted index per city are out of scope here — but a senior names that the linear scan in search is the obvious scaling cost and that a per-city price-sorted structure is the next step.

Solution.javaauto-runs ~0.8s after you pause
unverifiedEdited since last clean run — run to verify.

Tests

○ Not run yet — edit the code or press Run
  • by_id
  • by_id_missing
  • search_filter
  • search_rank
  • + 2 hidden tests — revealed once the visible ones pass

Pair partner (병신 모드 · 버그 잡기)

gpt-4o-mini

Ask for a skeleton or a test plan — not the answer. Then implement the core yourself and come back for edge cases. Remember: the partner is fallible. Verify everything by running it.

The partner sees your editor but cannot change it. Copy any suggestion in by hand, then Run to verify.

AI랑 먼저 페어하고 눌러