Select an Object by a Dynamic Integer Field
BridgeSoftware Engineer · Technical ScreenLast updated
Implement a clean, defensive utility function to select a dictionary from a collection based on the minimum or maximum value of a dynamic integer field, properly handling missing keys and ties.
Problem Statement
You are asked to implement a utility function that selects a single object (represented as a dictionary) from a collection based on a specified field name and an extreme condition (MIN or MAX).
Each object is a mapping of string keys to integer values. Crucially, records are semi-structured: not every object is guaranteed to contain the requested field.
Your function should:
- Filter for records that contain the target
field. - Find and return the record whose value for
fieldis either the minimum or maximum, depending onmode. - In the event of a tie, return the first record encountered in the original sequence.
- If no records contain the specified
field, or the input list is empty, returnNone. - Raise a
ValueErrorifmodeis neither"MIN"nor"MAX"(case-insensitive).
Function Signature
from typing import Any
def select_by_field(
records: list[dict[str, int]],
field: str,
mode: str = "MAX"
) -> dict[str, int] | None:
pass
Constraints
0 <= len(records) <= 100,0000 <= len(record.keys()) <= 50for each object- Integer values can be negative, zero, or positive (
-10^9 <= value <= 10^9). - The algorithm should operate in
O(N)time andO(1)auxiliary space, whereNis the number of records.
Examples
Example 1
records = [
{"a": 10, "b": 20},
{"a": 5, "c": 15},
{"a": 10, "d": 99}
]
# Call:
select_by_field(records, field="a", mode="MAX")
# Output: {"a": 10, "b": 20}
# Explanation: Both records at index 0 and 2 have value 10 for "a". Index 0 is returned to break ties by first occurrence.
Example 2
records = [
{"id": 1, "score": -5},
{"id": 2},
{"id": 3, "score": -20},
{"id": 4, "score": 0}
]
# Call:
select_by_field(records, field="score", mode="MIN")
# Output: {"id": 3, "score": -20}
# Explanation: The record with id 2 does not have "score" and is ignored. -20 is the minimum among [-5, -20, 0].
Example 3
records = [{"x": 1}, {"y": 2}]
# Call:
select_by_field(records, field="z", mode="MAX")
# Output: None
# Explanation: No record contains key "z".
Hints
Hint 1
Make sure to verify that the field key exists in a dictionary before accessing or comparing its value.
Hint 2
A single linear pass keeping track of the `best_record` and `best_value` avoids creating unnecessary intermediate lists or sorting overhead.
Hint 3
Be careful when initializing your tracking variables: using `float('-inf')` or `float('inf')` works, or you can initialize tracking state using the first valid record encountered.
Follow-up questions
- How would you refactor this to process an unbounded generator or stream of records without holding all records in memory?
- How would you support secondary tie-breaker fields if two records have identical values for the primary field?
- If this function is queried millions of times over a static set of records, what precomputed index would you build to achieve sub-linear query time?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.