Katkat

Parse logs and generate NFT attributes

CoinbaseSoftware Engineer · Onsite

Last 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

  1. Parsing: Extract events. Ignore lines that do not match the expected format.
  2. Grouping: Group unique ATTRIBUTE_VALUEs under their corresponding ATTRIBUTE_NAMEs.
  3. Validation: An NFT is only valid if it contains exactly one value from each unique ATTRIBUTE_NAME found in the logs.
  4. Weighted Sampling: Given a target list of ATTRIBUTE_NAMEs, write a function that returns a random valid NFT combination where the probability of selecting an ATTRIBUTE_VALUE is 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"]

  • BACKGROUND options: {"BLUE": 2, "RED": 1}
  • EYES options: {"LASER": 1}
  • A valid NFT would be {"BACKGROUND": "BLUE", "EYES": "LASER"} (66% chance) or {"BACKGROUND": "RED", "EYES": "LASER"} (33% chance).

Constraints

  • logs length up to 10,000.
  • ATTRIBUTE_NAME and ATTRIBUTE_VALUE strings contain only alphanumeric characters.
  • If an attribute in target_attributes is missing from the logs, return None.

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)?