← Questions

Group email logs by sender and sort them

0
StripeCodingLast asked May 2026

A coding interview question asked in Stripe's HackerRank assessment, last asked May 2026. It tests sorting, parsing and hash map.

Each email log line holds a timestamp, a sender address, a receiver address and a subject. Read the logs from standard input, group them by sender, and print each sender's logs in sorted order.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • group_logs(stdin: str) -> list[str] — the input's first line is n, then n lines <timestamp> <sender> <receiver> <subject>. The timestamp is a non-negative integer, the addresses have no spaces, and the subject is the rest of the line after the third field: it may contain spaces or be empty. Return the output lines:
    • Senders in lexicographic order, each introduced by sender: <sender>.
    • Under it, that sender's logs as <timestamp> <receiver> <subject>, sorted by timestamp (numerically), then receiver, then subject.

Example

input:
5
100 alice@a.com bob@b.com hello
90 alice@a.com carl@c.com meeting notes
90 dan@d.com e@e.com hi
90 alice@a.com bob@b.com a-subject
100 dan@d.com a@a.com ping

output:
sender: alice@a.com
90 bob@b.com a-subject
90 carl@c.com meeting notes
100 bob@b.com hello
sender: dan@d.com
90 e@e.com hi
100 a@a.com ping

Constraints

  • 1 <= n <= 2 * 10^5, 0 <= timestamp <= 10^9, each line at most 300 characters.

Clarifying questions

  • Should spaces inside the subject be kept exactly, including runs of spaces?
  • With an empty subject, is there a trailing space on the output line?
  • Is the timestamp compared as a number (so 90 comes before 100)?
  • Can the same log appear twice, and should duplicates be kept?

What a strong answer covers

  • Splitting each line at most three times (split(" ", 3)), so the subject keeps its spaces and an empty subject works.
  • Grouping in a dict of lists, then sorting senders and each group with a tuple key (int(ts), receiver, subject).
  • O(n log n) time.
  • Fast input reading for 2 × 10^5 lines.
  • Tests: empty subject, subjects with spaces, equal timestamps, timestamps of different lengths.

Follow-ups

  • The file does not fit in memory. Sort it with an external merge sort.
  • Add a filter by time window or receiver domain.
  • Output the top k senders by volume instead.

Where it's asked

What it tests

sorting · parsing · hash map

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding