← Questions

Python lists, dicts and concurrency

0
AppleCodingLast asked Jul 2025

A coding interview question asked in Apple's technical phone screen, last asked Jul 2025. It tests python, data structures and concurrency.

A Python fundamentals conversation: how lists and dictionaries behave and what their operations cost, then the GIL and choosing between threads and processes.

Questions

  1. Compare lists and dictionaries: common operations and their average time complexity, iteration order, mutability, and memory use.
  2. Transform a list with map and with a list comprehension. When is each the better choice?
  3. What is CPython's Global Interpreter Lock, and how does it affect multithreaded code?
  4. When would you use threading, and when multiprocessing?
  5. How do you share data between threads or processes safely (Queue, Lock, Event), and avoid race conditions and deadlocks?

Example

nums = [1, 2, 3, 4]
list(map(lambda x: x * x, nums))      # [1, 4, 9, 16]
[x * x for x in nums if x % 2 == 0]   # [4, 16]  (a comprehension can also filter)

What a strong answer covers

  • A list is a dynamic array: O(1) index and amortized append, O(n) insert or delete at the front and membership checks. A dict is a hash table: average O(1) get, set and membership; insertion order is kept since Python 3.7; keys must be hashable.
  • Memory: lists over-allocate; dicts carry hash table overhead.
  • Comprehensions are usually clearer and can filter; map is lazy and neat with an existing function.
  • The GIL lets one thread run Python bytecode at a time, so threads help I/O-bound work but not CPU-bound work; processes give true parallelism at the cost of pickling and memory.
  • x += 1 on shared data is not atomic; use a Lock, or pass work through a Queue; avoid deadlocks with a fixed lock order and timeouts.

Follow-ups

  • Why can't a list be a dict key, and what can you use instead?
  • A CPU-bound job runs slower with 8 threads than with 1. Explain.
  • When would you reach for asyncio instead of threads?

Where it's asked

  • Apple · Technical phone screen · Last asked Jul 2025

What it tests

python · data structures · concurrency

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