Python interviews for support and platform roles in banks are rarely about clever algorithms. Interviewers want to see that you can write a reliable script under pressure: parse a log, query a database, call an API, and handle the failure cases cleanly. This guide covers the questions that come up most often, with short model answers.
What the interviewer is really testing
- Automation instinct: can you turn a manual runbook step into a safe script?
- Data handling: files, CSV, JSON, dates and timezones (a constant source of trading bugs).
- Defensive coding: exceptions, logging, retries and idempotency.
- Readability: code that the next person on the rota can support at 3am.
Core questions and model answers
1. What is the difference between a list and a tuple?
Lists are mutable; tuples are immutable and hashable (if their contents are), so a tuple can be a dictionary key. Use tuples for fixed records such as (trade_id, book, notional).
2. Explain mutable default arguments.
Default values are evaluated once, when the function is defined. A default of [] is shared across calls and silently accumulates data. Use None and create the list inside the function.
def add_trade(trade, batch=None):
if batch is None:
batch = []
batch.append(trade)
return batch
3. How do you read a very large log file without running out of memory?
Iterate over the file object line by line, or use a generator. Never call read() on a multi-gigabyte file.
def errors(path):
with open(path, encoding="utf-8", errors="replace") as f:
for line in f:
if "ERROR" in line:
yield line.rstrip()
for e in errors("/var/log/app/pricing.log"):
print(e)
4. What is a generator and why use one?
A function that uses yield to produce values lazily. It keeps memory flat, which matters when processing market data or end-of-day trade files.
5. How do you handle exceptions properly?
Catch specific exceptions, log with context, and re-raise if you cannot recover. Avoid a bare except:, which hides real failures and swallows KeyboardInterrupt.
import logging
log = logging.getLogger(__name__)
try:
rows = fetch_positions(book)
except TimeoutError:
log.warning("Positions timed out for %s, retrying", book)
rows = fetch_positions(book)
except Exception:
log.exception("Unexpected failure for %s", book)
raise
6. Why should you not use floats for money?
Binary floats cannot represent most decimal fractions exactly, so 0.1 + 0.2 != 0.3. Use decimal.Decimal with explicit rounding rules for cash amounts. This is a favourite question in finance.
7. How do you deal with dates and timezones?
Store and compare in UTC using timezone-aware datetimes (zoneinfo), and convert to local time only for display. Mention daylight saving changes: London, New York and Tokyo markets shift on different dates, which breaks naive schedulers.
8. What does the GIL mean for your scripts?
Only one thread executes Python bytecode at a time. Threads still help for I/O-bound work (API calls, DB queries); for CPU-bound work use multiprocessing or concurrent.futures.ProcessPoolExecutor.
9. How would you query a database safely from Python?
Use the driver’s parameter binding, never string formatting, to avoid SQL injection. Use a context manager so connections and cursors are closed.
import oracledb
with oracledb.connect(user=u, password=p, dsn=dsn) as conn:
with conn.cursor() as cur:
cur.execute(
"SELECT trade_id, status FROM trades WHERE book = :book",
book="FI_RATES",
)
for trade_id, status in cur:
print(trade_id, status)
10. What are decorators? Give a practical example.
A decorator wraps a function to add behaviour. A retry decorator for flaky downstream calls is a good, relevant example.
import functools, time
def retry(times=3, delay=2):
def wrap(fn):
@functools.wraps(fn)
def inner(*a, **kw):
for attempt in range(1, times + 1):
try:
return fn(*a, **kw)
except Exception:
if attempt == times:
raise
time.sleep(delay * attempt)
return inner
return wrap
11. How do you manage dependencies and environments?
Virtual environments (venv), pinned versions in requirements.txt or a lock file, and an internal package mirror (Artifactory or Nexus) because bank servers rarely reach the public internet.
Scenario questions
These separate candidates. Talk through your approach before you write code.
- “Count trade rejections per counterparty from today’s FIX log.” Parse tag 35 (MsgType) and tag 58 (Text), group with
collections.Counter, output the top ten. - “Reconcile two CSV files of positions and report breaks.” Load both into dictionaries keyed by position ID, compare quantities with a tolerance, report missing and mismatched rows. Mention
pandasmerges withindicator=Trueas an alternative. - “Write a health check that polls five service endpoints and alerts if any fail.” Use
requestswith timeouts, run checks concurrently with a thread pool, exit non-zero on failure so the scheduler or monitoring tool picks it up.
Final tips
- Always mention logging, timeouts and exit codes. That is how production scripts fail safely.
- Know
collections,itertools,pathlib,subprocess,json,csvanddatetimewell. - Have one story ready about a script you wrote that saved the team time or prevented an incident, with numbers.