Extract all email addresses from a nested payload

Python data engineering interview problem. Difficulty: intermediate. Pattern: Recursion. About 15 minutes. Part of the Pro drill bank.

A webhook payload nests email addresses several layers down. A support tool should collect every address without the caller knowing the shape.

A privacy audit needs every email address hidden anywhere inside a nested payload: dictionaries, lists and plain values mixed together. Walk through the payload and collect all strings that look like an email address (text with an @ and a dot after it, no spaces). Return the distinct addresses as a sorted list. Write collect_emails(payload). From {'a': '[email protected]', 'b': ['no', {'c': '[email protected]'}]} the result is ['[email protected]', '[email protected]']. From {} the result is []. The starter file already has the function name and its print lines. Keep them and fill in the body.

Requirements

  • Look inside every dictionary value and every list item.
  • Keep only strings that match an email shape.
  • Return each address once.
  • Sort the result alphabetically.

Constraints

  • The payload is made of dictionaries, lists and simple values.
  • An address looks like [email protected] with no spaces.

Examples

Input: collect_emails({'a': '[email protected]', 'b': ['no', {'c': '[email protected]'}]}) Output: ['[email protected]', '[email protected]'] This input follows the stated rules and produces this output.

Topics: lakebench, python, nested-structures.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Extract all email addresses from a nested payload

Interview-style drill: Given an arbitrarily nested dict/list structure, collect every value that looks like an email address.

Extract all email addresses from a nested payload (dict/list mix).