Guide Python Intermediate

12.19 Interview Questions

Frequently asked and scenario-based Python set interview questions covering set vs. frozenset, why {} is a dict and not a set, hashability, and coding problems like finding elements common to three lists.

2 min read

Conceptual Questions

  • What’s the difference between a set and a frozenset?
  • Why can’t a list be an element of a set?
  • Why is {} a dict and not a set?
  • What’s the time complexity of membership testing in a set, and why?
  • What’s the difference between remove() and discard()?

Scenario-Based Questions

  • Membership needs to be checked against a large collection thousands of times — what data structure would be used, and why?
  • A set of sets (a group of groups) is needed — why can’t regular sets be used as the inner elements, and what would be used instead?
  • Two lists represent “expected” and “actual” server inventories — how would the missing and extra entries be found, in one or two lines?

Coding Questions

  • Write a function that returns the unique elements common to three different lists.
  • Write a function that deduplicates a list while preserving original order (hint: sets alone won’t do this).
  • Given two sets of firewall rules, write code to report rules present in only one of them.
def common_to_three(a, b, c):
    return set(a) & set(b) & set(c)

>>> common_to_three([1, 2, 3], [2, 3, 4], [2, 3, 5])
{2, 3}

Quick Interview Answer

“Set questions cluster around three areas: the set-vs-frozenset distinction and why immutability buys hashability; why {} means dict and every set element must itself be hashable, which rules out lists and dicts as members; and the practical set-operator patterns — intersection for what’s common across collections, difference (run both directions) for what’s missing versus extra between an expected and actual inventory. The deduplicate-while-preserving-order question is the one that catches people off guard: a plain set discards order entirely, so dict.fromkeys(lst) — not set(lst) — is the answer whenever order needs to survive.”

Common Mistakes

  • Answering “sets and lists are basically the same, just pick either” without mentioning uniqueness, hashability requirements, or ordering.
  • Solving “common elements in three lists” with nested loops instead of chaining & across three sets in one expression.
  • Forgetting that set(lst) alone does not preserve order when asked to deduplicate a list while keeping the original sequence — dict.fromkeys(lst) is the actual answer.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form