Home β€Ί Problems β€Ί Set union and intersection

Set union and intersection

🧠 Predict the output Topic: sets Difficulty: easy Free

What this problem practises

Sets are unordered containers that drop duplicates on insertion. Membership tests are fast, insertion order is not preserved, and {} builds an empty dict β€” not an empty set.

This is an output-prediction problem: read the snippet carefully and write down exactly what it prints before anything runs. These questions are interview staples because they test whether you know the language rules, or merely recognise their syntax.

Difficulty: easy β€” the fix or the trick is normally a single line, and a careful read should be enough.

The challenge

What will this program print? Type the exact output (both lines).

The code runs in PyDebug’s in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.

The snippet

The interactive editor pre-fills exactly this code β€” read it, then submit your prediction:

a = {3, 4, 5, 7}
b = {5, 7, 8, 1}
print(len(a | b))
print(sorted(a & b))
Solve it in the interactive editor β†’

More sets problems

Keep going