Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Two services logged a message about the same incident. A reviewer wants the words that are unusual: words that appear exactly once across both messages together. These are the parts where the two messages really differ.
Write uncommon_words(first, second). Both are strings of words separated by whitespace. Return a sorted list of the words that appear exactly once in the two messages combined.
Function to write
uncommon_words(first: str, second: str) -> list[str]A sorted list of the lowercase words that appear exactly once across both messages together.
How to approach it
Put the words of both messages into one counter. Keep the words whose total count is 1.
Sample cases
+ 3 held back until you submit
two log messages
`disk`, `full`, `on` and `node` each appear twice in total. Only `a` and `b` appear once.
Input
Argument 1
'disk full on node a'Argument 2
'disk full on node b'Returns
[
'a',
'b'
]a word repeated inside one message
`retry` appears twice, both in the first message. It must not be returned. `timeout` and `success` appear once.
Input
Argument 1
'retry retry timeout'Argument 2
'success'Returns
[
'success',
'timeout'
]same capital letters do not matter
The three spellings of error are the same word, so it appears three times and is not returned.
Input
Argument 1
'Error ERROR error'Argument 2
'warn'Returns
[
'warn'
]two empty messages
There are no words at all, so the answer is an empty list.
Input
Argument 1
''Argument 2
''Returns
[] (empty list)
Constraints
Error and error are the same word. Return the words in lowercase.Worked example
Take 'retry retry timeout' and 'success'. Lowercase and split: retry, retry, timeout, then success. Count all four together: retry 2, timeout 1, success 1. Keep the words with a count of 1: success and timeout. Sorted, the answer is ['success', 'timeout'].
The trap: comparing the two sets of words (words in one but not the other) keeps retry. It is not in the second message, even though it appears twice.
What this tests
Counting words across two inputs, the idea behind 'Uncommon Words from Two Sentences'. The key is to count both messages in the same Counter and keep the count of 1. Data engineers do the same when they compare tokens in two log lines or two column lists.
uncommon_words(first: str, second: str) -> list[str]Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.