← Questions
Python lists, dicts and concurrency
0
I was asked this
No reports yet. Sign in to say where you were asked it.
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
- Compare lists and dictionaries: common operations and their average time complexity, iteration order, mutability, and memory use.
- Transform a list with
mapand with a list comprehension. When is each the better choice? - What is CPython's Global Interpreter Lock, and how does it affect multithreaded code?
- When would you use
threading, and whenmultiprocessing? - 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;
mapis 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 += 1on shared data is not atomic; use aLock, or pass work through aQueue; 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
asyncioinstead of threads?
Where it's asked
- Apple · Technical phone screen · Last asked Jul 2025
What it tests
python · data structures · concurrency
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
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
