Recently, I appeared for an interview, and I am sharing the questions and answers that were asked during the session.
1. Fibonacci Series in Python
The Fibonacci series is a sequence in which each number is the sum of the two preceding numbers.
Example (Loop Method):
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
Example (List Generation):
fib = [0, 1]
for i in range(2, 10):
fib.append(fib[-1] + fib[-2])
2. List Comprehension vs Tuple Comprehension
List Comprehension
A concise way to create a list.
squares = [x*x for x in range(10)]
✔ Stores all values in memory.
Tuple Comprehension (Generator Expression)
Python does not have tuple comprehension.
But you can write:
t = (x*x for x in range(10))
This creates a generator, not a tuple.
To convert to a tuple:
t = tuple(x*x for x in range(10))
3. What is a Generator?
A generator is a function or expression that returns values one at a time using the yield keyword.
Example:
def my_gen():
for i in range(5):
yield i
Why Generators?
- Save memory (don’t store all values)
- Faster for large datasets
- Used in streaming, pipelines, and big data
4. SQL Query to Find 2nd Highest Salary
Method 1: Using ORDER BY + LIMIT (MySQL/PostgreSQL)
SELECT salary
FROM employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
Method 2: Using MAX() Function
SELECT MAX(salary) AS second_highest
FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
If the salary table is separate
SELECT amount
FROM salary
ORDER BY amount DESC
LIMIT 1 OFFSET 1;
5. How Python Manages Memory?
Python uses multiple internal systems:
Private Heap Memory
All Python objects and data structures live in a private heap.
Memory Manager
Allocates space for objects automatically.
Garbage Collector
Uses:
- Reference counting
- Cycle detection
When no reference objects are deleted.
Object Caching Mechanism
Python caches:
- Small integers
- Short strings
To improve performance.
6. How to Use Oracle Database with Python
To connect Python with Oracle, use the cx_Oracle module.
Install the library:
pip install cx_Oracle
Connection Example:
import cx_Oracle
conn = cx_Oracle.connect(
user="username",
password="password",
dsn="localhost/orcl" # host/service_name
)
cur = conn.cursor()
cur.execute("SELECT * FROM employees")
for row in cur:
print(row)
cur.close()
conn.close()
Why cx_Oracle?
- Fast
- Supports stored procedures
- Supports bulk insert
- Official Oracle driver
7. How do you handle if the Django Project suddenly gets high traffic?
When Django receives sudden high traffic, I handle it by using caching to reduce server load, adding a load balancer to distribute requests, and scaling the application by running multiple instances. I also optimize database queries, move heavy tasks to background workers, use a CDN for static files, and monitor the system to detect issues early.

