yoklainterview sim

Backend Python Stdlib Http Interview Questions

75 verified Backend Python Stdlib Http interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Python Stdlib HttpDifficulty 1
from collections import defaultdict

counts = defaultdict(int)
words = ["a", "b", "a", "c", "b", "a"]
for w in words:
    counts[w] += 1

print(dict(counts))

What does this print?
  • a{'a': 3, 'b': 2, 'c': 1}
  • b{'a': 1, 'b': 1, 'c': 1}
  • cRaises KeyError on the first access to counts["a"]
  • d{'a': 3, 'b': 2, 'c': 1, None: 0}
Explanation:defaultdict(int) supplies 0 (the result of calling int()) for any missing key instead of raising KeyError, so counts[w] += 1 works even the first time a word is seen. No extra keys like None are ever added — only keys that were actually looked up appear.
Python Stdlib HttpDifficulty 2
You need to group a list of (category, value) pairs into a dict mapping each category to a list of its values, without writing an explicit "if key not in dict" check before every append.
from collections import defaultdict

pairs = [("fruit", "apple"), ("veg", "carrot"), ("fruit", "banana")]
groups = defaultdict(list)
for cat, val in pairs:
    groups[cat].append(val)

What does defaultdict(list) do here that a plain dict would not?
  • aIt behaves identically to a plain dict; append still raises KeyError on the first item of each new category.
  • bIt pre-populates groups with every category name found anywhere in the running program.
  • cOn first access to a missing key, it calls list() to create an empty list and binds it to that key, so append works immediately.
  • dIt silently discards any category not already present when groups was created.
Explanation:defaultdict(list) invokes the factory (list()) via __missing__ whenever a key is looked up and not found, producing a fresh empty list bound to that key on the spot. That is exactly why groups[cat].append(val) can run without a preceding existence check — a plain dict would raise KeyError on the first append for a new category.
Python Stdlib HttpDifficulty 1
from collections import Counter

tally = Counter(["red", "blue", "red", "red", "blue"])
print(tally["red"], tally["green"])

What is printed?
  • a3 None
  • b3 0
  • cRaises KeyError because "green" was never counted
  • d2 0
Explanation:Counter is a dict subclass whose __getitem__ returns 0 for missing keys instead of raising KeyError, so tally["green"] is 0 without ever being inserted. "red" appears 3 times in the source list, so tally["red"] is 3.
Python Stdlib HttpDifficulty 2
from collections import Counter

tally = Counter({"a": 3, "b": 3, "c": 1})
print(tally.most_common(1))

"a" and "b" are tied for the highest count. What determines which one appears in the result?
  • amost_common always breaks ties alphabetically, so "a" is guaranteed to come first.
  • bmost_common raises ValueError when two counts are tied, since the ordering would be ambiguous.
  • cPython re-sorts tied keys randomly on every call, so the result is nondeterministic across runs.
  • dmost_common uses a stable sort by count only, so ties keep the relative order the keys had when first inserted into the Counter.
Explanation:Counter.most_common() sorts purely by count using a stable sort, so elements with equal counts keep whatever relative order they already had (here, insertion order from the dict literal: "a" then "b"). It never breaks ties alphabetically or randomly, and it never raises on ties.
Python Stdlib HttpDifficulty 1
from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x, p[1], p.y == p[1])

What does this print?
  • a3 4 False
  • bRaises TypeError because namedtuple fields cannot be accessed by index
  • c3 4 True
  • d4 3 True
Explanation:A namedtuple supports both attribute access (p.x, p.y) and positional index access (p[0], p[1]) for the same underlying tuple values, since it is a regular tuple subclass with named accessors added. p.x is 3, p[1] equals p.y which is 4, so the equality check is True.
Python Stdlib HttpDifficulty 1
from collections import namedtuple

Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
p.x = 10

What happens when the last line runs?
  • aIt succeeds silently, updating p.x to 10, since namedtuple fields are ordinary mutable attributes.
  • bIt raises AttributeError, because namedtuple instances are tuples under the hood and tuples are immutable.
  • cIt succeeds, but only the copy of p accessible through p.x changes, not p[0].
  • dIt raises TypeError at class-definition time when Point was first created, not when p.x is assigned.
Explanation:namedtuple generates a regular tuple subclass with read-only properties for named field access; since tuples are immutable, attempting p.x = 10 raises AttributeError ("can't set attribute"). To get an updated instance you must use p._replace(x=10), which returns a new object instead of mutating p.

Test yourself against the 3300-question Backend bank.

Start interview