Interview questions
Frequently asked questions with concise model answers. Try answering aloud before expanding.
What is the difference between a list and a tuple? Easy
Lists are mutable (can change) and use []; tuples are immutable and use (). Tuples are hashable (if their items are) so they can be dict keys, and are slightly faster and lighter.
Explain `*args` and `**kwargs`. Easy
*args collects extra positional arguments into a tuple; **kwargs collects extra keyword arguments into a dict. They make functions flexible and are common in decorators and wrappers.
What are decorators? Medium
A decorator is a function that takes a function and returns a new function, adding behaviour without modifying the original — e.g. logging, caching (functools.lru_cache), auth checks (@login_required).
What is the GIL? Hard
CPython's Global Interpreter Lock allows only one thread to execute Python bytecode at a time. CPU-bound work should use multiprocessing (or native extensions); I/O-bound work benefits from threads or asyncio.
Shallow vs deep copy? Medium
A shallow copy (copy.copy, list[:]) copies the outer container but shares nested objects. A deep copy (copy.deepcopy) recursively copies everything.
Difference between WHERE and HAVING? Easy
WHERE filters rows before grouping; HAVING filters groups after aggregation. WHERE can't use aggregate functions; HAVING can.
What is an index and when can it hurt? Medium
An index (usually a B-tree) speeds up lookups and joins. It costs disk space and slows INSERT/UPDATE/DELETE because the index must be maintained. Avoid over-indexing low-selectivity columns.
Explain normalisation. Medium
Organising tables to reduce redundancy: 1NF atomic values, 2NF no partial dependency on a composite key, 3NF no transitive dependencies. Analytical systems often denormalise for speed.
loc vs iloc? Easy
loc selects by labels (index names, column names, boolean arrays); iloc selects by integer positions.
How do you handle missing data? Medium
Inspect with isna().sum(), then drop (dropna), fill with a constant/mean/median/mode (fillna), forward-fill for time series, or impute with a model — choosing based on why data is missing.
Explain the bias–variance trade-off. Medium
Bias: error from overly simple assumptions (underfitting). Variance: sensitivity to training data (overfitting). Increasing model complexity lowers bias but raises variance; we aim for the sweet spot via validation, regularisation and more data.
What is cross-validation? Easy
Splitting data into k folds, training on k−1 and validating on the remaining fold, rotating k times. It gives a more reliable performance estimate than a single split.
Precision vs recall? Easy
Precision = TP / (TP + FP): of predicted positives, how many are correct. Recall = TP / (TP + FN): of actual positives, how many we found. F1 is their harmonic mean.
How does a random forest work? Medium
It trains many decision trees on bootstrapped samples with random feature subsets, then averages (regression) or votes (classification). This reduces variance compared to a single tree.
What is dropout? Medium
A regularisation technique that randomly zeros a fraction of neurons during training, forcing the network to learn redundant representations and reducing overfitting.
Why use ReLU over sigmoid in hidden layers? Medium
ReLU doesn't saturate for positive inputs, mitigating vanishing gradients, and is cheap to compute — leading to faster training in deep networks.
How would you reduce hallucinations in an LLM app? Medium
Ground answers with RAG, instruct the model to cite sources and say 'I don't know', lower temperature, validate outputs against schemas or databases, and evaluate with a test set of known Q&A.
What is prompt injection? Hard
Malicious input that tries to override the system's instructions (e.g. 'ignore previous instructions'). Mitigate by separating untrusted content, limiting tool permissions, validating outputs and never exposing secrets to the model.
What is the Django ORM's N+1 problem? Medium
Looping over objects and accessing a related field triggers one query per object. Fix with select_related (FK/OneToOne JOIN) or prefetch_related (separate query for M2M/reverse FK).
How does Django protect against common attacks? Medium
Auto-escaping templates (XSS), CSRF tokens, parameterised ORM queries (SQL injection), clickjacking headers, secure password hashing, and security settings like HSTS and secure cookies.