Implement, Refactor, Extend: Handling Changing Requirements
Adapt to a mid-interview requirement change in the Palantir FDSE coding round by implementing, refactoring, and then extending working code. Record a requirement delta, separate a behavior-preserving refactor from the intentional change, and verify each at its own checkpoint against unchanged regression tests. Add a per-site cap that scans past the old prefix, write exact-output tests that expose prefix-first bugs, and summarize progress honestly when time runs short.
We'll cover the following...
- What should you confirm before editing select_alerts?
- Which promises change when the site cap appears?
- Where can you refactor without changing the answer?
- Why must capped selection scan beyond the old answer?
- Which assertions expose a plausible broken patch?
- What should you preserve when time runs short?
- Can you extend the independent b-alert baseline?
“Keep the ranking, but add an optional maximum per site so one site cannot occupy the whole operator queue.” Your select_alerts function already returns globally ranked active alerts when the interviewer interrupts with this request. The challenge is to preserve the working behavior while changing exactly the selection rule. Treat this as a course practice scenario, not a reported company question.
What should you confirm before editing select_alerts?
Start with two consequential questions: “Is the site maximum a hard cap even if that leaves unused queue slots?” and “Does omitting the cap, or passing None, preserve the original behavior?” Here, both answers are yes.
A requirement delta separates added or changed behavior from promises that still apply. Record it before editing; when the interviewer replaces an assumption, update the contract instead of defending the assumption.
Palantir identifies working within existing systems as an engineering competency, including modifying and extending existing code. The supplied summary of its official careers guidance supports practicing those skills; it does not establish a guaranteed implement-refactor-extend interview sequence.
Separate the behavior-preserving refactor from the intentional behavior change, and check each at its own checkpoint.
Which promises change when the site cap appears?
The baseline contract for select_alerts(alerts, limit=3) is:
Trusted records: Unique nonempty string
alert_idvalues, nonempty stringsite_idvalues, Booleanactivefields, and integerseverityvalues from 1 through 3. Record-schema validation is a precondition.Limit: A positive integer. Boolean and non-integer values raise
TypeError; nonpositive integers raiseValueError, even on empty input.Selection: Retain active alerts, rank globally by
(-severity, alert_id), and return at mostlimitresults.Output: Fresh dictionaries containing exactly
alert_id,site_id, andseverity. Preserve input order and contents.
The extension adds keyword-only per_site_limit=None. A supplied cap follows the same positive-integer validation rules as limit. All original promises survive; capped calls add site-count eligibility, and the hard cap can produce fewer than limit results.
Save this complete Python 3.12 baseline as alert_selection_baseline.py:
def select_alerts(alerts, limit=3):if isinstance(limit, bool) or not isinstance(limit, int):raise TypeError("limit must be an integer, not bool")if limit <= 0:raise ValueError("limit must be positive")active = [alert for alert in alerts if alert["active"]]ranked = sorted(active, key=lambda alert: (-alert["severity"], alert["alert_id"]))return [{"alert_id": alert["alert_id"],"site_id": alert["site_id"],"severity": alert["severity"],}for alert in ranked[:limit]]
Lines 1–5: Validate the caller’s limit before processing any records, including an ...