Debugging in Python - Interview Questions and Answers
Debugging is the process of identifying and fixing errors in a program to ensure it runs correctly.
- Syntax Errors
- Runtime Errors
- Logical Errors
Syntax errors occur when Python cannot understand a line of code. They must be fixed by correcting the syntax before running the script.
A traceback is the error message generated when an exception occurs, showing the sequence of function calls leading to the error.
print()statementsloggingmodulepdb(Python Debugger)tracemodule
print() helps check variable values and track execution flow without modifying the program significantly.
It can clutter the code and make debugging inefficient for complex applications.
The logging module provides different log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL) and allows saving logs to a file.
Breakpoints pause the execution of the program at a specific line, allowing the developer to inspect variable values.
import pdb
pdb.set_trace()This pauses execution at the pdb.set_trace() line.
The Python Debugger (pdb) allows stepping through code, setting breakpoints, and inspecting variable values during execution.
python -m pdb script.pyThis runs the script in debug mode.
n(next line)s(step into function)c(continue execution)q(quit debugger)
assert is used for debugging by checking if a condition is True, raising an AssertionError if it is False.
assert is used for debugging and can be disabled in production, whereas exceptions should be handled explicitly.
Using try-except blocks to catch exceptions and handle them properly.
finally ensures that certain code runs regardless of whether an exception occurs or not.
An infinite loop occurs when a loop never terminates. Debugging involves adding print statements, breakpoints, or using pdb.
Using tools like gc (garbage collection module) and objgraph.
cProfile is used to analyze the performance of code and identify bottlenecks.
python -m cProfile script.py
It prints the complete traceback of an exception, helping debug errors inside except blocks.
Using the inspect module to retrieve function details at runtime.
Using the threading module with logging or a debugger that supports multi-threading.
It helps debug segmentation faults by printing detailed stack traces when crashes occur.
Using logging.exception() inside except blocks to capture error details.
It occurs when Python interacts with invalid memory addresses, often due to issues in C extensions.
Using gdb (GNU Debugger) with Python for advanced debugging.
A core dump captures memory state when a program crashes and can be analyzed using gdb.
Optimizing recursion depth and using iterative approaches instead.
Using memory_profiler and tracemalloc modules.
Tracking memory allocations in Python programs.
Using cProfile, line_profiler, and timeit modules.
It is a sampling profiler for monitoring Python performance.
Using %%timeit in Jupyter or cProfile.
Relying on logging and assertions instead of actively debugging.
Using Django Debug Toolbar and built-in logging.
It allows stepping through code interactively, such as with pdb or ipdb.
Using Flask’s built-in debugger and logging.
It allows debugging after an exception occurs using pdb.pm()
Using %debug magic command.
It dynamically modifies modules at runtime, making debugging harder.
Using sys.setrecursionlimit() and tracking function calls.
By detecting issues early and preventing regressions.
A testing framework that provides better debugging tools.
Using remote debugging tools like debugpy
It provides better traceback information when one exception leads to another.
Using gc.collect() and gc.get_objects()
Using Sentry or Rollbar for error monitoring.
They store logs in a structured format (JSON) for better analysis.
Problem:
The following function is supposed to count down from n to 0 but runs forever.
def countdown(n):
while n > 0:
print(n)
# Missing decrement step
print("Done!")
countdown(5)Debugging & Solution:
The function is stuck in an infinite loop because n is never decremented.
def countdown(n):
while n > 0:
print(n)
n -= 1 # Fix: decrement n
print("Done!")
countdown(5)
Problem:
The function below is supposed to calculate the factorial of a number, but it crashes with a RecursionError.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n) # Missing base case for negative numbers
print(factorial(-5))Debugging & Solution:
We need to handle negative inputs properly.
def factorial(n):
if n < 0:
raise ValueError("Factorial is not defined for negative numbers") # Fix
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Problem:
The function is supposed to return the sum of numbers from 1 to n, but it gives incorrect results.
def sum_n(n):
total = 0
for i in range(1, n):
total += i # The loop excludes n
return total
print(sum_n(5)) # Expected 15, but returns 10Debugging & Solution:
The loop should include n.
def sum_n(n):
total = 0
for i in range(1, n + 1): # Fix: include n
total += i
return total
print(sum_n(5)) # Correct output: 15
Problem:
The following function is supposed to return the length of a list but fails with a TypeError.
def list_length(lst):
return len(lst) + 1 # Error when lst is None
print(list_length(None))Debugging & Solution:
We need to check for None before calling len().
def list_length(lst):
if lst is None:
return 0 # Fix: Handle None case
return len(lst) + 1
print(list_length(None)) # Correct output: 0
Problem:
The function tries to fetch a key from a dictionary but crashes.
def get_price(prices, item):
return prices[item] # KeyError if item is missing
prices = {"apple": 100, "banana": 50}
print(get_price(prices, "orange")) # KeyErrorDebugging & Solution:
Use .get() with a default value.
def get_price(prices, item):
return prices.get(item, "Item not found") # Fix: Handle missing keys
print(get_price(prices, "orange")) # Correct output: "Item not found"
Problem:
This script tries to read a file that does not exist.
def read_file(filename):
with open(filename, "r") as f:
return f.read()
print(read_file("missing.txt")) # FileNotFoundErrorDebugging & Solution:
Use try-except to handle missing files.
def read_file(filename):
try:
with open(filename, "r") as f:
return f.read()
except FileNotFoundError:
return "File not found" # Fix: Handle missing file
print(read_file("missing.txt")) # Correct output: "File not found"
Problem:
The function is checking floating-point equality incorrectly.
def is_equal(a, b):
return a == b
print(is_equal(0.1 + 0.2, 0.3)) # Expected True, but returns FalseDebugging & Solution:
Use math.isclose() for floating-point comparisons.
import math
def is_equal(a, b):
return math.isclose(a, b, rel_tol=1e-9) # Fix: Use isclose
print(is_equal(0.1 + 0.2, 0.3)) # Correct output: True
Problem:
The function takes too long to compute the sum of a list.
def slow_sum(lst):
total = 0
for i in range(len(lst)):
total += lst[i] # Slow due to unnecessary indexing
return total
nums = list(range(10**6))
print(slow_sum(nums))Debugging & Solution:
Use sum() for better performance.
def fast_sum(lst):
return sum(lst) # Fix: Use built-in sum()
print(fast_sum(nums)) # Much faster
Problem:
The function tries to access an index that may be out of range.
def get_item(lst, index):
return lst[index] # IndexError if index is too large
print(get_item([1, 2, 3], 5)) # IndexErrorDebugging & Solution:
Check the index before accessing.
def get_item(lst, index):
if index >= len(lst):
return "Index out of range" # Fix: Handle out-of-range index
return lst[index]
print(get_item([1, 2, 3], 5)) # Correct output: "Index out of range"
Problem:
The function is failing when trying to parse an invalid JSON string.
import json
def parse_json(data):
return json.loads(data) # Fails if JSON is invalid
print(parse_json("{invalid json}")) # JSONDecodeErrorDebugging & Solution:
Use try-except to catch errors.
import json
def parse_json(data):
try:
return json.loads(data)
except json.JSONDecodeError:
return "Invalid JSON" # Fix: Handle JSON errors
print(parse_json("{invalid json}")) # Correct output: "Invalid JSON"
Tutorials
Random Blogs
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Datasets for Exploratory Data Analysis for Beginners
- Top 15 Recommended SEO Tools
- Understanding Data Lake, Data Warehouse, Data Mart, and Data Lakehouse – And Why We Need Them
- How to Become a Good Data Scientist ?
- Understanding LLMs (Large Language Models): The Ultimate Guide for 2025
- Ideas for Content of Every niche on Reader’s Demand during COVID-19
- 10 Awesome Data Science Blogs To Check Out
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- Deep Learning (DL): The Core of Modern AI
