Code Room
CodingHardcod-g897
Subject Dp on intervalsLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given a sorted list of distinct keys with access frequencies 'freq' (freq[i] is how often key i is searched), build a binary search tree that minimizes the total expected search cost. The cost of an arrangement is the sum over all keys of freq[key] * (depth-of-key, counting the root as depth 1). Return the minimum possible total cost. 1 <= len(freq) <= 250, 0 <= freq[i] <= 10^5.

Implement
optimal_bst(freq: list[int]) → int
Examples
in[[34,8,50]]out142
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.