|
| 1 | +<br> |
| 2 | +<br> |
| 3 | +<br> |
| 4 | +<br> |
| 5 | +<br> |
| 6 | + |
| 7 | + |
| 8 | +# Python Programming Interview Questions (2026) |
| 9 | + |
| 10 | +## Advanced Questions That Test Real Engineering Thinking |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +<p align = "right"><b>Written by</b>: <i>Tanu Nanda Prabhu</i></p> |
| 16 | + |
| 17 | +Most Python interview guides focus on syntax and beginner tricks. However, modern interviews in **2026** evaluate something deeper, **how well you understand Python's internals, performance trade-offs, and real-world design decisions.** |
| 18 | + |
| 19 | +Below are **advanced Python interview questions with concise explanations and code examples** that reflect the type of discussions happening in serious engineering interviews today. |
| 20 | + |
| 21 | +<br> |
| 22 | +<br> |
| 23 | +<br> |
| 24 | +<br> |
| 25 | +<br> |
| 26 | +<br> |
| 27 | +<br> |
| 28 | +<br> |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +# 1. What actually happens when Python executes a function call? |
| 34 | + |
| 35 | +Python does more than simply "run a function". Internally it creates a **new stack frame**, manages local variables, and pushes execution to the **Python Virtual Machine (PVM)**. |
| 36 | + |
| 37 | +### Key Concepts |
| 38 | +- Python compiles `.py` code into **bytecode** |
| 39 | +- Bytecode runs on the **Python Virtual Machine** |
| 40 | +- Each function call creates a **frame object** |
| 41 | + |
| 42 | +### Example |
| 43 | + |
| 44 | +```python |
| 45 | +def add(a, b): |
| 46 | + return a + b |
| 47 | + |
| 48 | +add(3, 4) |
| 49 | +``` |
| 50 | + |
| 51 | +Internally Python performs steps similar to: |
| 52 | + |
| 53 | +1. Create stack frame |
| 54 | +2. Bind `a=3`, `b=4` |
| 55 | +3. Execute bytecode |
| 56 | +4. Return result |
| 57 | +5. Destroy frame |
| 58 | + |
| 59 | +You can inspect the bytecode using: |
| 60 | + |
| 61 | +```python |
| 62 | +import dis |
| 63 | + |
| 64 | +def add(a, b): |
| 65 | + return a + b |
| 66 | + |
| 67 | +dis.dis(add) |
| 68 | +``` |
| 69 | + |
| 70 | +Understanding this helps explain recursion depth limits and function call overhead. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +<br> |
| 75 | +<br> |
| 76 | +<br> |
| 77 | +<br> |
| 78 | + |
| 79 | +# 2. What is the difference between `__new__` and `__init__`? |
| 80 | + |
| 81 | +Many developers know `__init__`, but advanced interviews expect knowledge of **object creation mechanics**. |
| 82 | + |
| 83 | +### Concept |
| 84 | + |
| 85 | +| Method | Responsibility | |
| 86 | +| :-------- | :------------------------------ | |
| 87 | +| `__new__` | Creates the object instance | |
| 88 | +| `__init__` | Initializes the created object | |
| 89 | + |
| 90 | +### Example |
| 91 | + |
| 92 | +```python |
| 93 | +class Demo: |
| 94 | + |
| 95 | + def __new__(cls): |
| 96 | + print("Creating instance") |
| 97 | + return super().__new__(cls) |
| 98 | + |
| 99 | + def __init__(self): |
| 100 | + print("Initializing instance") |
| 101 | + |
| 102 | +obj = Demo() |
| 103 | +``` |
| 104 | + |
| 105 | +### Output |
| 106 | + |
| 107 | +```text |
| 108 | +Creating instance |
| 109 | +Initializing instance |
| 110 | +``` |
| 111 | + |
| 112 | +### When is `__new__` useful? |
| 113 | +- Implementing **Singleton pattern** |
| 114 | +- Subclassing **immutable types** |
| 115 | +- Custom memory allocation |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +<br> |
| 120 | +<br> |
| 121 | +<br> |
| 122 | +<br> |
| 123 | +<br> |
| 124 | +<br> |
| 125 | + |
| 126 | +# 3. Explain Python's Global Interpreter Lock (GIL) |
| 127 | + |
| 128 | +The GIL ensures that only one thread executes Python bytecode at a time within a process. |
| 129 | + |
| 130 | +### Why it exists |
| 131 | + |
| 132 | +- Simplifies memory management |
| 133 | +- Protects reference counting |
| 134 | +- Prevents race conditions inside the interpreter |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | +### Example showing limitation |
| 139 | + |
| 140 | +```python |
| 141 | +import threading |
| 142 | + |
| 143 | +def task(): |
| 144 | + for _ in range(10_000_000): |
| 145 | + pass |
| 146 | + |
| 147 | +t1 = threading.Thread(target=task) |
| 148 | +t2 = threading.Thread(target=task) |
| 149 | + |
| 150 | +t1.start() |
| 151 | +t2.start() |
| 152 | + |
| 153 | +t1.join() |
| 154 | +t2.join() |
| 155 | +``` |
| 156 | + |
| 157 | +Even with two threads, CPU-heavy tasks do not run truly in parallel. |
| 158 | + |
| 159 | +### Real-world solutions |
| 160 | + |
| 161 | +- `multiprocessing` |
| 162 | +- `asyncio` |
| 163 | +- Native extensions (C/C++) |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +# 4. What are Python descriptors? |
| 168 | + |
| 169 | +Descriptors power many advanced Python features such as: |
| 170 | + |
| 171 | +- properties |
| 172 | +- methods |
| 173 | +- class attributes |
| 174 | +- ORM frameworks |
| 175 | + |
| 176 | +<br> |
| 177 | +<br> |
| 178 | + |
| 179 | +A descriptor implements any of: |
| 180 | + |
| 181 | +- `__get__` |
| 182 | +- `__set__` |
| 183 | +- `__delete__` |
| 184 | + |
| 185 | +## Example |
| 186 | + |
| 187 | +```python |
| 188 | +class PositiveNumber: |
| 189 | + |
| 190 | + def __get__(self, instance, owner): |
| 191 | + return instance._value |
| 192 | + |
| 193 | + def __set__(self, instance, value): |
| 194 | + if value < 0: |
| 195 | + raise ValueError("Value must be positive") |
| 196 | + instance._value = value |
| 197 | + |
| 198 | + |
| 199 | +class Account: |
| 200 | + balance = PositiveNumber() |
| 201 | + |
| 202 | +acc = Account() |
| 203 | +acc.balance = 100 |
| 204 | +``` |
| 205 | + |
| 206 | +This allows **fine-grained control over attribute access**. |
| 207 | + |
| 208 | +--- |
| 209 | + |
| 210 | +# 5. How does Python manage memory? |
| 211 | + |
| 212 | +Python uses two main mechanisms: |
| 213 | + |
| 214 | +### 1. Reference Counting |
| 215 | + |
| 216 | +Each object tracks how many references point to it. |
| 217 | + |
| 218 | +```python |
| 219 | +import sys |
| 220 | + |
| 221 | +a = [] |
| 222 | +print(sys.getrefcount(a)) |
| 223 | +``` |
| 224 | + |
| 225 | +<br> |
| 226 | +<br> |
| 227 | +<br> |
| 228 | +<br> |
| 229 | +<br> |
| 230 | + |
| 231 | + |
| 232 | +### 2. Garbage Collector |
| 233 | + |
| 234 | +Handles cyclic references that reference counting cannot clean. |
| 235 | + |
| 236 | +```python |
| 237 | +import gc |
| 238 | +gc.collect() |
| 239 | +``` |
| 240 | + |
| 241 | +### Important interview insight |
| 242 | + |
| 243 | +Memory leaks can occur when: |
| 244 | + |
| 245 | +- objects participate in reference cycles |
| 246 | +- `__del__` methods are used improperly |
| 247 | + |
| 248 | +--- |
| 249 | + |
| 250 | +# 6. What is the difference between generators and iterators? |
| 251 | +Iterator |
| 252 | + |
| 253 | +An object implementing: |
| 254 | + |
| 255 | +- `__iter__` |
| 256 | +- `__next__` |
| 257 | + |
| 258 | +### Generator |
| 259 | + |
| 260 | +A simpler way to create iterators using yield. |
| 261 | + |
| 262 | +### Example |
| 263 | + |
| 264 | +```json |
| 265 | +def count_up_to(n): |
| 266 | + i = 1 |
| 267 | + while i <= n: |
| 268 | + yield i |
| 269 | + i += 1 |
| 270 | + |
| 271 | +for number in count_up_to(3): |
| 272 | + print(number) |
| 273 | +``` |
| 274 | + |
| 275 | +Advantages of generators: |
| 276 | + |
| 277 | +- Lazy evaluation |
| 278 | +- Lower memory usage |
| 279 | +- Ideal for large datasets and streaming |
| 280 | + |
| 281 | +--- |
| 282 | + |
| 283 | +# 7. What are metaclasses in Python? |
| 284 | + |
| 285 | +**A metaclass defines how classes themselves are created.** |
| 286 | + |
| 287 | +If objects are instances of classes, |
| 288 | +then **classes are instances of metaclasses.** |
| 289 | + |
| 290 | +### Example |
| 291 | + |
| 292 | +```python |
| 293 | +class Meta(type): |
| 294 | + |
| 295 | + def __new__(cls, name, bases, attrs): |
| 296 | + attrs['version'] = "1.0" |
| 297 | + return super().__new__(cls, name, bases, attrs) |
| 298 | + |
| 299 | + |
| 300 | +class App(metaclass=Meta): |
| 301 | + pass |
| 302 | + |
| 303 | + |
| 304 | +print(App.version) |
| 305 | +``` |
| 306 | + |
| 307 | +Metaclasses are commonly used in: |
| 308 | + |
| 309 | +- ORMs |
| 310 | +- frameworks |
| 311 | +- API validation systems |
| 312 | + |
| 313 | +--- |
| 314 | + |
| 315 | +# Final Thoughts |
| 316 | + |
| 317 | +Modern Python interviews are no longer about remembering syntax. They focus on: |
| 318 | + |
| 319 | +- **Understanding Python internals** |
| 320 | +- **Performance trade-offs** |
| 321 | +- **Writing scalable and maintainable systems** |
| 322 | + |
| 323 | +If you can confidently explain topics like: |
| 324 | + |
| 325 | +- descriptors |
| 326 | +- metaclasses |
| 327 | +- the GIL |
| 328 | +- memory management |
| 329 | +- generators |
| 330 | + |
| 331 | +then you are already operating at a **senior Python engineering level**. |
| 332 | + |
| 333 | +> Keep building. Keep experimenting. Python rewards those who explore its depths. |
0 commit comments