Parse logs and generate NFT attributes
CoinbaseSoftware Engineer · OnsiteLast updated
This problem tests your ability to parse structured log strings, group events by metadata, and perform weighted sampling across combinations of attributes.
Problem Statement
You are given a list of raw log strings, each representing an event: "[TIMESTAMP] EVENT_TYPE: ATTRIBUTE_NAME=ATTRIBUTE_VALUE". Your task is to process these logs to generate potential NFT attribute combinations.
Requirements
- Parsing: Extract events. Ignore lines that do not match the expected format.
- Grouping: Group unique
ATTRIBUTE_VALUEs under their correspondingATTRIBUTE_NAMEs. - Validation: An NFT is only valid if it contains exactly one value from each unique
ATTRIBUTE_NAMEfound in the logs. - Weighted Sampling: Given a target list of
ATTRIBUTE_NAMEs, write a function that returns a random valid NFT combination where the probability of selecting anATTRIBUTE_VALUEis proportional to its frequency in the logs.
Function Signature
def generate_nft(logs: list[str], target_attributes: list[str]) -> dict:
pass
Example
Input logs:
["[10:00] ACTION: BACKGROUND=BLUE", "[10:01] ACTION: BACKGROUND=RED", "[10:02] ACTION: BACKGROUND=BLUE", "[10:05] ACTION: EYES=LASER"]
Input target_attributes: ["BACKGROUND", "EYES"]
BACKGROUNDoptions:{"BLUE": 2, "RED": 1}EYESoptions:{"LASER": 1}- A valid NFT would be
{"BACKGROUND": "BLUE", "EYES": "LASER"}(66% chance) or{"BACKGROUND": "RED", "EYES": "LASER"}(33% chance).
Constraints
logslength up to 10,000.ATTRIBUTE_NAMEandATTRIBUTE_VALUEstrings contain only alphanumeric characters.- If an attribute in
target_attributesis missing from the logs, returnNone.
Hints
Hint 1
Use a dictionary of dictionaries (or a dictionary of Counter objects) to store the frequency counts for each attribute type.
Hint 2
When parsing, use string splitting or regex to isolate the key-value pair after the colon.
Hint 3
For weighted random selection, use `random.choices` with the list of values and their corresponding frequency weights.
Follow-up questions
- How would you optimize the memory usage if the log file size exceeds the available RAM?
- What changes are needed if an NFT requires a minimum number of attributes but can contain optional ones?
- How would you handle logs if the attribute values could be hierarchical (e.g., BACKGROUND:COLOR=BLUE instead of BACKGROUND=BLUE)?
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.