Brute force was the obvious first move and I said so, but she shut that down fast and asked for an inverted index approach.
Start by clarifying the query semantics and metadata structure, then design an inverted index that maps each field-value pair to the set of config names. For nested fields, flatten the paths (e.g., 'key.subkey') and index each leaf value, handling arrays by indexing each element. Finally, intersect the posting lists for all query conditions to efficiently retrieve matching configs.
Pro tip: Discuss how to handle defaults and missing fields by either including default values in the index or treating absent fields as matching default queries, and mention the trade-off between index size and query speed.
Ask questions to understand the exact query language, metadata schema, default values, and expected scale. Define how nested fields and arrays are represented and queried.
Choose a mapping from field-value pairs to sets of config names. For nested structures, use dot-separated paths; for arrays, index each element individually. Consider using a hash map for O(1) lookups.
Iterate over all configs and their metadata, recursively flatten nested objects and arrays, and add the config name to the posting list for each field-value pair. Handle defaults by either indexing default values or noting their absence.
Parse the JSON query into a set of field-value conditions. For each condition, retrieve the posting list from the index. If a field is unspecified, treat it as a default condition (e.g., match all or match default value).
Compute the intersection of all posting lists to get configs matching all conditions. Optimize by starting with the smallest list. Return the resulting configs, ensuring they meet any default criteria.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.