Code Room
CodingMediumcod-g1167
Subject Database query optimizerLevel Mid–Senior~26 minCommon in Databases & SQL interviewsIndustries Software development

Question

Implement a tiny cost-based optimizer decision for a single-predicate query. Given `total_rows`, the estimated `matching_rows` for the predicate, the per-row cost of a full table scan `scan_cost`, and the per-matching-row cost of an index lookup `index_cost`, compute both plan costs: full_scan_cost = total_rows * scan_cost, and index_cost_total = matching_rows * index_cost. Return the string 'index' if the index plan is strictly cheaper, otherwise 'scan' (ties favor 'scan', since a sequential scan avoids random I/O). All inputs are non-negative integers.

Implement
choose_plan(total_rows: int, matching_rows: int, scan_cost: int, index_cost: int) → str
Examples
in[1000,5,1,3]out"index"
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.