1. What happens when you type a URL in a browser and hit Enter?
Answer:
“When I enter a URL, the browser first checks the cache or DNS to resolve the domain name
to an IP. Then it establishes a TCP connection with the server, usually using a three-way
handshake. An HTTP/HTTPS request is sent, the server processes it, and responds with the
requested data (HTML, JSON, etc.). The browser then renders the response into a webpage.
If HTTPS is used, SSL/TLS handshake happens before data transfer for encryption.”
2. How does garbage collection work in Python?
Answer:
“Python uses reference counting as its primary mechanism. Each object keeps track of how
many references point to it. When the count reaches zero, the object is deallocated. Python
also has a cyclic garbage collector that cleans up objects involved in reference cycles that
reference counting alone can’t handle.”
3. What is the difference between multiprocessing and multithreading in Python given
the GIL?
Answer:
“In Python, due to the Global Interpreter Lock (GIL), only one thread executes Python
bytecode at a time, which limits true parallelism in multithreading for CPU-bound tasks.
That’s why multithreading is better for I/O-bound tasks. For CPU-bound tasks,
multiprocessing is preferred because each process has its own Python interpreter and
memory space, bypassing the GIL and achieving real parallelism.”
4. How does SQL indexing work internally?
Answer:
“Indexes are like lookup tables that improve query speed. Most databases use B-trees or
variants like B+ trees for indexing. Instead of scanning the entire table, the database can
quickly navigate the tree to find the required row. The tradeoff is that indexes consume extra
space and slow down write operations since the index must be updated along with the table.”
5. What’s the difference between REST and GraphQL APIs?
Answer:
“REST provides fixed endpoints that return structured data, often more than needed.
GraphQL allows clients to query exactly what they need in a single request. REST is simpler
and widely adopted, but GraphQL reduces over-fetching and under-fetching of data.
However, GraphQL requires more complex server-side setup.”
REST (Representational State Transfer) is an architectural style for designing APIs. It
uses standard HTTP methods like GET, POST, PUT, DELETE to work with resources,
which are represented by endpoints (URLs). For example, GET /users/1 fetches details of
user with ID 1. REST is simple, widely adopted, and easy to cache, but sometimes it causes
over-fetching or under-fetching of data because the response structure is fixed by the
server.
GraphQL, on the other hand, is a query language for APIs developed by Facebook.
Instead of multiple endpoints, it has a single endpoint, and the client specifies exactly what
data it needs. For example, I can ask for a user’s name and only their post titles in a single
query. This avoids over-fetching and under-fetching, and gives flexibility. However,
GraphQL is more complex to set up and requires careful query optimization to prevent
performance issues.
6. Explain deadlock conditions and how to prevent them.
Answer:
“Deadlock occurs when four conditions hold simultaneously:
1. Mutual Exclusion – a resource can’t be shared.
2. Hold and Wait – a process holds a resource while waiting for others.
3. No Preemption – a resource can’t be forcibly taken.
4. Circular Wait – processes wait in a cycle.
To prevent deadlocks, we can break at least one of these conditions. For example, use
preemption, enforce resource ordering, or avoid hold-and-wait by requesting all resources at
once.”
7. Explain how HashMap works internally in Java/Python dictionary.
Answer:
“A HashMap (or Python dict) uses a hash function to convert keys into an index in an array.
Each index points to a bucket that holds key-value pairs. In case of collisions, Python uses
open addressing and dynamic resizing. Lookups are O(1) on average, but in worst case with
too many collisions, it can degrade to O(n).”
8. What are CAP theorem tradeoffs in distributed systems?
Answer:
“CAP theorem states that in any distributed system, we can only guarantee two out of three:
Consistency, Availability, and Partition Tolerance.
Consistency: All nodes see the same data.
Availability: Every request gets a response.
Partition Tolerance: The system works even if nodes are disconnected.
For example, MongoDB sacrifices consistency for availability, while traditional SQL
databases prioritize consistency.”
9. How does memory management work in C vs Java vs Python?
Answer:
C: Manual memory management using malloc/free. Faster but error-prone (memory
leaks, dangling pointers).
Java: Automatic garbage collection (mark-and-sweep).
Python: Reference counting + garbage collector for cycles. Easier for developers but
slightly less performant compared to manual memory management.
10. How would you optimize a slow SQL query?
Answer:
“I’d start by analyzing the execution plan to see where the bottleneck is. Then I’d consider
adding indexes, rewriting queries to avoid SELECT *, breaking down large joins into
smaller ones, and denormalizing data where needed. Also, caching results and using
partitioning on large tables can help.”
1. What are Python’s key features?
💬 Answer: Python is an interpreted, high-level, dynamically typed language. It’s known for
its simplicity and readability. It supports multiple paradigms like object-oriented, procedural,
and functional programming. Python also has extensive libraries for data science, web
development, and AI, making it versatile.
2. Difference between lists, tuples, sets, and dictionaries?
💬 Answer:
List → ordered, mutable, allows duplicates.
Tuple → ordered, immutable, allows duplicates.
Set → unordered, mutable, no duplicates.
Dictionary → key-value pairs, mutable, keys are unique.
3. Explain Python’s memory management and garbage collection.
💬 Answer: Python uses reference counting and a cyclic garbage collector to manage memory.
When an object’s reference count drops to zero, memory is automatically freed. The garbage
collector also detects and cleans up cyclic references.
4. What are decorators in Python?
💬 Answer: A decorator is a function that modifies another function or method without
changing its code. It’s often used for logging, authentication, or caching. For example,
@staticmethod is a built-in decorator.
5. Difference between shallow copy and deep copy?
💬 Answer: A shallow copy copies only references to nested objects, so changes affect both.
A deep copy creates completely independent copies of all objects, using the copy.deepcopy()
function.
6. Generators vs Iterators?
💬 Answer: An iterator is any object with __iter__() and __next__() methods. A generator is
a function that yields values using yield, creating an iterator automatically. Generators are
memory-efficient since they produce values lazily.
7. is vs == in Python?
💬 Answer: == checks value equality, while is checks object identity (whether two references
point to the same memory location).
8. Global, local, and nonlocal variables?
💬 Answer: Local variables are defined inside a function. Global variables exist in the main
scope. nonlocal is used inside nested functions to refer to variables from the enclosing scope.
9. Explain Python’s GIL.
💬 Answer: The Global Interpreter Lock ensures only one thread executes Python bytecode at
a time, even on multi-core processors. This simplifies memory management but can limit
true multithreading. For CPU-bound tasks, multiprocessing is preferred.
10. Mutable vs Immutable objects?
💬 Answer: Mutable objects like lists and dicts can be changed after creation. Immutable
objects like strings and tuples cannot be altered; any modification creates a new object.