We have curated a list of all Python Interview Questions which are recently asked in Accenture company. These Accenture Python Interview Questions consist interview question with detailed answers.
1. What is Python? What are its key features?
Answer:
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum. Key features include:
-
Readability and simplicity (clean syntax)
-
Dynamically typed
-
Interpreted (executes bytecode via Python VM)
-
Large standard library (“batteries included”)
-
Support for multiple paradigms: procedural, OOP, functional
-
Extensible (can interface with C/C++), embeddable
-
Strong community and ecosystem (packages via PyPI)
2. What is PEP 8 and why is it important?
Answer:
PEP 8 is the Python Enhancement Proposal #8, a style guide for writing idiomatic, readable Python code. It prescribes naming conventions, indentation, line length, whitespace rules, imports order, etc. Following PEP 8 makes code more maintainable and comprehensible by teams.
3. What’s the difference between is and == in Python?
Answer:
-
==checks for value equality (i.e. whether the objects have equal content, via__eq__) -
ischecks for identity, i.e. whether they refer to the same object in memory
Example:
4. Explain how memory management works in Python.
Answer:
Python uses automatic memory management via:
-
Reference counting — every object tracks how many references point to it; when count drops to zero, it’s deallocated
-
Garbage collector — handles cyclic references (objects referring to each other) by periodically detecting unreachable cycles and freeing them
-
Private heap — all Python objects and data live in Python’s private heap, managed by the interpreter
-
Memory pools / arenas — for small objects, Python uses memory pooling to reduce fragmentation
5. What are mutable and immutable types? Give examples.
Answer:
-
Immutable types: their content cannot change after creation. Examples:
int,float,str,tuple,frozenset. -
Mutable types: can be changed in-place. Examples:
list,dict,set,bytearray.
Because immutable objects cannot change, operations that “modify” them actually produce new objects (e.g. string concatenation).
6. Explain shallow copy vs deep copy.
Answer:
-
Shallow copy: copies the container object, but references to nested (inner) objects are shared. So changes in nested mutable objects reflect in both copies.
-
Deep copy: recursively copies all nested objects, so the copy is fully independent.
Python has a copy module:
7. What is a decorator? Give an example.
Answer:
A decorator is a higher-order function that takes a function and returns a modified function (or wraps behavior around it) without modifying its code. They are often used for logging, access control, caching, instrumentation, etc.
Example:
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Before call")
result = func(*args, **kwargs)
print("After call")
return result
return wrapper
@my_decorator
def say_hello(name):
print(f”Hello, {name}”)
say_hello(“Alice”)
8. What is the difference between @staticmethod and @classmethod?
Answer:
-
@staticmethod: a method that doesn’t receive an implicit first argument (no
selforcls). It behaves like a plain function placed inside a class for organizational reason. -
@classmethod: receives the class (
cls) as the first argument, and can access/modify class state.
Example:
def class_method(cls, val):
return cls(val)
def __init__(self, v):
self.v = v
9. What is a generator and how is it different from a normal function?
Answer:
A generator is a function that yields values one at a time, pausing its state between yields, when you call next() on it. It is memory-efficient for large data sequences because it doesn’t build the entire list in memory.
Differences:
-
Use
yieldinstead ofreturn. -
Generators implement the iterator protocol (
__iter__and__next__). -
They resume from last yield point, maintaining local state.
Example:
g = my_gen(5)
next(g) # 0
next(g) # 2
10. How do *args and **kwargs work in function definitions?
Answer:
-
*argscollects extra positional arguments as a tuple. -
**kwargscollects extra keyword arguments as a dict.
Example:
f(1, 2, 3, 4, x=10, y=20)
# Output:
# 1 2
# (3, 4)
# {‘x’: 10, ‘y’: 20}
They’re useful for flexible APIs and forwarding arguments.
11. What is the Global Interpreter Lock (GIL) in CPython? How does it affect multi-threading?
Answer:
GIL is a mutex in CPython that ensures only one thread executes Python bytecode at a time. This means that multi-threading in Python is less effective for CPU-bound tasks (they often run serially). However, for I/O-bound tasks (network, file I/O), threads can still be useful because while one thread waits on I/O, another can run.
To bypass GIL for CPU-bound work, you can use:
-
Multiprocessing (separate processes)
-
Use external libraries (C extensions) that release GIL
-
Use async/await / event-driven concurrency
12. Explain Python’s exception handling (try / except / else / finally).
Answer:
-
try: block to wrap code that might raise exceptions -
except: catches and handles specified exception(s) -
else: executes if no exception was raised intry -
finally: always executes, whether exception occurred or not (useful for cleanup)
Example:
13. What is monkey patching in Python?
Answer:
Monkey patching refers to dynamically modifying or extending code at runtime (e.g. overriding methods or attributes) of existing modules or classes without altering the source. It is sometimes used for testing or hotfixes, but should be used cautiously.
Example:
14. What is name mangling in Python? When is it used?
Answer:
Name mangling is how Python deals with class private attributes. If a class attribute name starts with at least two underscores (and does not end with two underscores), Python rewrites it internally to include the class name, e.g., __var becomes _ClassName__var. This helps avoid name collisions in subclasses.
Used for “pseudo private” to avoid accidental override, not true privacy.
15. Why does Python not support method overloading like Java/C++?
Answer:
In Python, if you define multiple methods with the same name but different signatures (parameters), the last definition overrides previous ones. Python uses duck typing rather than static overloading. Instead, you can use default arguments, variable arguments (*args, **kwargs), or single dispatch (functools) to mimic overloading behavior.
16. What is a context manager and the with statement? How do you implement one?
Answer:
A context manager is a protocol with __enter__ and __exit__ methods used to encapsulate setup and teardown logic (e.g. opening/closing files). The with statement invokes these automatically.
Example:
def __exit__(self, exc_type, exc_val, exc_tb):
print(“Exiting”)
with MyContext() as ctx:
print(“Inside context”)
Or use contextlib to simplify.
17. Explain how to create and use virtual environments in Python.
Answer:
Virtual environments isolate dependencies per project rather than installing globally.
-
To create:
python3 -m venv envname -
To activate:
On Linux/macOS:source envname/bin/activate
On Windows:envname\Scripts\activate -
To deactivate:
deactivate -
You can also use tools like
virtualenv,pipenv,poetry.
Within a virtual environment, pip install installs packages to the project scope only.
18. What is the difference between list.sort() and sorted()?
Answer:
-
list.sort()is an in-place method: it modifies the original list and returnsNone. -
sorted()is a built-in function: it returns a new sorted list, leaving the original untouched.
Both accept arguments like key and reverse.
19. How can you reverse a string in Python?
Answer (multiple ways):
-
Slicing:
s[::-1] -
Using
reversed()and join:''.join(reversed(s)) -
Looping or stack (less common)
Example:
20. Write a function to check if a string is a palindrome (ignoring spaces, punctuation, case).
Answer (sample):
21. What is a lambda function? Give an example where it’s used.
Answer:
A lambda is an anonymous single-expression function defined with the lambda keyword.
Example:
# Or inline usage:
squares = list(map(lambda x: x*x, [1, 2, 3, 4]))
Often used for short callback functions (e.g. key functions in sort, map/filter).
22. Explain what list comprehensions are and give an example.
Answer:
List comprehensions provide a concise way to create lists. They typically follow the form:
Example:
This is more readable and often faster than equivalent loops.
23. How do you sort a dictionary by value, not by key?
Answer:
Alternatively, using operator.itemgetter:
24. Explain how to iterate over two lists in parallel.
Answer:
Use the zip() function:
for x, y in zip(list1, list2):
print(x, y)
If the lengths differ, zip stops at the shortest. Use itertools.zip_longest to continue with a fill value.
25. What are Python modules and packages?
Answer:
-
A module is a
.pyfile containing Python definitions (functions, classes, variables) that you can import. -
A package is a directory containing modules and a special
__init__.pyfile, helping structure modules in nested namespace hierarchies.
You import modules with import module or from module import X.
26. What is dynamic typing? How does it differ from static typing?
Answer:
Dynamic typing means the type of variable is determined at runtime, not declared explicitly. Variables can refer to objects of any type, and the same variable can be reassigned to different types.
Static typing (e.g. in Java, C++) requires declaring variable types at compile time and enforces type constraints.
Example:
27. Explain the difference between append() vs extend() in lists.
Answer:
-
append(x)adds one element (objectx) to the end of the list. -
extend(iterable)iterates over the iterable and appends each element to the list.
Example:
28. How do you handle missing keys in a dictionary (i.e. default values)?
Answer:
-
Use
dict.get(key, default)which returnsdefaultif key not present -
Use
collections.defaultdictfor automatic default values -
Use
setdefault(key, default)to insert default if not present
Example:
29. What is the difference between filter(), map(), and reduce()?
Answer:
-
map(func, iterable)appliesfuncto each element and returns an iterator of results -
filter(func, iterable)returns elements for whichfunc(element)is True -
reduce(func, iterable)(fromfunctools) reduces the iterable to a single value by cumulatively applyingfunc
Example:
30. How would you remove duplicates from a list while maintaining order?
Answer:
One approach:
Alternatively, in Python 3.7+ (dict preserves insertion order):
31. What are Python’s built-in data types?
Answer:
Common data types include:
-
Numeric:
int,float,complex -
Sequence:
list,tuple,range -
Text:
str -
Mapping:
dict -
Set types:
set,frozenset -
Boolean:
bool -
Binary:
bytes,bytearray,memoryview -
NoneType:
None
32. What is the difference between __str__ and __repr__ methods?
Answer:
-
__repr__is meant to represent the object in an “official” way, ideally so thateval(repr(obj)) == obj. It is unambiguous. -
__str__is meant to be readable (user-facing) string representation.
If you only implement one, fallback is:
-
__repr__, if__str__not defined
Example:
def __repr__(self):
return f”Point({self.x}, {self.y})”
def __str__(self):
return f”({self.x}, {self.y})”
p = Point(1,2)
print(p) # calls __str__, prints “(1, 2)”
p # in REPL calls __repr__, prints “Point(1, 2)”
33. Write code to flatten a nested list, e.g. [[1,2], [3, [4,5]], 6].
Answer (recursive):
nested = [[1,2], [3, [4,5]], 6]
flat = list(flatten(nested))
print(flat) # [1,2,3,4,5,6]
34. What are metaclasses in Python?
Answer:
A metaclass is the “class of a class”; it’s the template that Python uses to create classes. You can customize class creation by defining a metaclass (via metaclass= in class definition). It allows controlling how classes behave, enforce patterns, modify class attributes, etc.
Example:
class MyClass(metaclass=MyMeta):
pass
obj = MyClass()
print(obj.added()) # “Added method”
35. What is monkey patching and when is it used?
(This is similar to question 13; intentionally repeating to test deeper understanding in interview)
Answer:
Monkey patching is dynamically modifying or extending classes or modules at runtime (after import). Use cases include:
-
Patching behavior for testing (mocking)
-
Hotfixing bugs without modifying original code
-
Hooking into external library behavior
Be careful: can lead to maintenance headaches or unexpected side effects.
36. Explain how you’d build a simple REST API in Python (e.g. using Flask). What should you consider for security and scalability?
Answer:
Steps and considerations:
-
Use a lightweight framework like Flask or FastAPI
-
Define endpoints (
@app.route) with methods (GET, POST, PUT, DELETE) -
Use blueprints / routers to modularize
-
Input validation and sanitization (e.g. using
pydantic,marshmallow) -
Use authentication/authorization (JWT, OAuth, API keys)
-
Handle error/exception centrally
-
Use pagination for large data sets
-
Use caching (Redis, in-memory)
-
Use rate limiting (prevent abuse)
-
Logging, monitoring, metrics
-
Use WSGI servers (Gunicorn, uWSGI) behind reverse proxy (nginx)
-
Use database connection pooling
-
Enable HTTPS / TLS, secure headers
-
Load balancing, horizontal scaling, stateless architecture (store state outside)
A candidate may be asked to sketch the API, show code, or discuss trade-offs.
37. What’s the difference between multiprocessing and multithreading? When would you use one or the other in Python?
Answer:
-
Multithreading: multiple threads in the same process, share memory. Useful for I/O-bound tasks. But due to GIL, CPU-bound threads are ineffective in CPython.
-
Multiprocessing: separate processes, separate memory space. Bypasses GIL, so good for CPU-bound tasks.
Use multithreading for I/O (network calls, file reads). Use multiprocessing for CPU-heavy tasks (math, data crunching).
38. What are namedtuple, deque, defaultdict, Counter in the collections module? Give use-cases.
Answer:
-
namedtuple: factory for tuple subclasses with named fields (accessible by name and index).
Use-case: lightweight immutable record (e.g. Point = namedtuple(“Point”, [“x”,”y”])) -
deque: double-ended queue with fast appends/pops from both ends.
Use-case: queue / stack, sliding windows -
defaultdict: dict that provides default values for missing keys (via a default factory). -
Counter: subclass of dict for counting hashable objects; useful to tally frequencies
39. What is method resolution order (MRO) in Python? How does it work in multiple inheritance?
Answer:
MRO is the order Python uses to look up methods/attributes in classes. Python 3 uses C3 linearization (C3 superclass linearization) for MRO. In multiple inheritance, it ensures a consistent order that respects class hierarchy and preserves monotonicity.
You can inspect ClassName.__mro__ or ClassName.mro() to see the order.
Example:
print(D.__mro__)
40. Explain how garbage collection of cyclic references works.
Answer:
Python’s GC module periodically runs a cyclic garbage collector which:
-
Identifies groups of objects that reference each other but are unreachable from root references
-
Breaks cycles and deallocates them
-
Uses generation-based collection: young, mid, old generations; more frequent collection in younger generations
You can interact via the gc module (e.g. gc.collect(), gc.get_objects()).
41. Describe how you would profile and optimize a slow Python application.
Answer:
-
Use profiling tools:
cProfile,profile,line_profiler,pyinstrument -
Identify hotspots (functions consuming most time)
-
Optimize algorithms or data structures (e.g. change complexity)
-
Use builtins or optimized libraries (NumPy, C-extensions)
-
Avoid repeated work, caching results (memoization)
-
Use concurrency / parallelism (multiprocessing, async)
-
Use just-in-time compilers (e.g. Numba) or alternative implementations (PyPy)
-
Reduce I/O bottlenecks (buffer, batching)
-
Use efficient data formats, avoid excessive copying
42. How would you implement a thread-safe singleton in Python?
Answer:
One approach using a metaclass with locking:
This ensures only one instance is created, even under concurrency.
43. How do you merge two sorted lists into one sorted list (without using built-ins like sorted)?
Answer (merge step of merge sort):
print(merge([1,3,5], [2,4,6])) # [1,2,3,4,5,6]
44. Suppose you have a list of integers, find the pair whose sum is closest to zero (or a target). How would you approach?
Answer (two-pointer approach after sorting):
Talk through edge-cases (duplicates, size < 2).
45. What is the output and reasoning of the following snippet?
print(f(2, 3))
print(f(3, 4))
print(f(4, 5, []))
print(f(5, 6))
Answer:
Output:
Reason: The default list L=[] is evaluated once at function definition. So calls without providing L reuse the same list. The third call passes a new empty list, so it doesn’t use the default. Hence the pattern.
46. What will be printed by this code?
class B(A):
def __init__(self):
super().__init__()
self.y = 2
obj = B()
print(obj.x, obj.y)
Answer:
It prints:
Because B.__init__() calls super().__init__() (the initializer of A) which sets x = 1, then B sets y = 2.
47. How do you handle JSON in Python? Convert a Python dict to JSON string and vice versa.
Answer:
Use the json module:
You can also use json.dump() / json.load() to work directly with files.
48. What is pickling / unpickling in Python?
Answer:
-
Pickling is serializing a Python object into a byte stream.
-
Unpickling is deserializing that byte stream back into a Python object.
You use the pickle module:
Be careful: unpickling untrusted data can be unsafe (arbitrary code execution).
49. Write a function to detect if a linked list has a cycle (Floyd’s Tortoise and Hare algorithm).
Answer (pseudo / Python):
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow is fast:
return True
return False
Explanation: The fast pointer moves twice as fast as slow. If there is a cycle, they will eventually meet.
50. How would you design an LRU cache in Python? (Implementation sketch)
Answer:
You can implement LRU (least recently used) cache using a combination of:
-
A doubly linked list (for O(1) insert/delete)
-
A hash map/dictionary mapping keys to nodes
Alternatively, use collections.OrderedDict (Python 3.7+ dict is ordered too) to simplify:
This gives O(1) average get and put performance.
51. What is the difference between __new__ and __init__?
Answer:
-
__new__(cls, *args, **kwargs)is a static (or class) method that actually creates and returns a new instance (i.e., it allocates memory). It’s called before__init__. -
__init__(self, *args, **kwargs)initializes the already created instance (sets up attributes, etc.).
If you override __new__, you must return an instance (usually via super().__new__(cls)).
52. What is the __slots__ mechanism and when would you use it?
Answer:__slots__ allows you to declare a fixed set of attributes for a class, preventing the creation of a per-instance __dict__. This can reduce memory usage and improve attribute access speed, especially if you have many instances.
Example:
But using __slots__ disallows dynamically adding new attributes and complicates multiple inheritance.
53. How does Python’s method resolution order (MRO) handle diamond inheritance (the “diamond problem”)?
Answer:
Python uses the C3 linearization algorithm for MRO (in Python 3). In the diamond inheritance pattern (A → B and A → C, then D inherits B and C), the MRO ensures each class appears only once and in a consistent “merge” order.
For example:
print(D.mro()) # => [D, B, C, A, object]
The order ensures that you don’t visit A twice, and respects the inheritance hierarchy.
54. Explain how Python’s weakref module works and give use cases.
Answer:
The weakref module provides “weak references” to objects, which do not increase the object’s reference count. When the object is garbage-collected (i.e. no strong references remain), the weak reference becomes invalid (calls a callback or yields None).
Use cases:
-
Caches or memoization where you don’t want the cache to prevent object destruction
-
Observer patterns
-
Avoiding memory leaks in circular references or registries
Example:
55. What are coroutines in Python (beyond just generators)? How do async / await work?
Answer:
Coroutines are functions that can suspend and resume execution, enabling asynchronous programming. In Python:
-
async def foo():defines a coroutine -
awaitis used to pause execution until an awaitable (another coroutine,asynciofuture, etc.) completes -
They allow you to write non-blocking code in a sequential style
Under the hood, asyncio or other event loops schedule and run coroutines. Coroutines enable high concurrency especially for I/O-bound tasks.
56. What is the difference between asyncio.Task and asyncio.Future?
Answer:
-
A Future is a low-level object representing an eventual result (placeholder) of an asynchronous operation.
-
A Task is a subclass of Future that wraps a coroutine and schedules its execution.
When you call asyncio.create_task(coro), you get a Task, which is also a Future, but it automatically starts running the coroutine in the event loop.
57. How would you limit the rate of requests (rate limiting) in a Python web service?
Answer:
Some strategies:
-
Use a leaky bucket or token bucket algorithm (maintain tokens, refill periodically)
-
Use a middleware or decorator that tracks request timestamps per user/IP and rejects beyond thresholds
-
Use an external store (Redis) to count requests and TTL keys
-
Combine with caching and throttling libraries (e.g.
ratelimit,limits) -
Enforce rate limits at the API gateway / reverse proxy (e.g. nginx)
Code sketch:
requests = {} # mapping key → [timestamps]
def rate_limit(key_fn, period, max_calls):
def decorator(fn):
)
def wrapper(*args, **kwargs):
key = key_fn(*args, **kwargs)
now = time.time()
timestamps = requests.get(key, [])
# drop old
timestamps = [t for t in timestamps if t > now – period]
if len(timestamps) >= max_calls:
raise Exception(“Too many requests”)
timestamps.append(now)
requests[key] = timestamps
return fn(*args, **kwargs)
return wrapper
return decorator
58. Explain how the Python Global Interpreter Lock (GIL) affects multiprocessing and threading.
Answer:
-
Threading: Because of the GIL, only one thread executes Python bytecode at a time in CPython. So CPU-bound tasks do not gain parallelism; only I/O-bound tasks benefit.
-
Multiprocessing: Each process has its own Python interpreter and memory space, so each has its own GIL. Thus,
multiprocessingcan bypass the GIL and achieve parallelism for CPU-bound tasks.
This is why for CPU-intensive workloads, multiprocessing, or offloading to C extensions or other languages, is often used.
59. Explain closure in Python with an example. How is it useful?
Answer:
A closure occurs when an inner function “remembers” variables from its enclosing scope even after the outer function has returned.
Example:
times3 = make_multiplier(3)
print(times3(10)) # 30
Here multiplier is a closure capturing n. Useful for factories, partial application, callbacks or configuring behavior dynamically.
60. What is the functools.lru_cache decorator? How does it work?
Answer:functools.lru_cache is a decorator that caches the results of function calls with given arguments (Least Recently Used cache). If the function is called again with the same args, the cached result is returned instead of recomputation.
It maintains a fixed-size cache and evicts least recently used items when full. Great for expensive recursive or pure functions.
Example:
61. How would you serialize Python objects to JSON where they contain non-serializable types (e.g. datetime, custom classes)?
Answer:
Approaches:
-
Provide a custom JSON encoder by subclassing
json.JSONEncoderand overridedefault(self, obj) -
Provide a
to_jsonorto_dictmethod in your class and manually convert -
Use libraries like
pydantic,marshmallow, ororjsonwhich support custom types -
Pre-convert the non-serializable fields (e.g.
datetime.isoformat())
Example:
62. Describe Python’s descriptor protocol. What are descriptors and when are they used?
Answer:
A descriptor is an object attribute with “binding behavior,” i.e. implements any of __get__, __set__, __delete__. Descriptors let you customize attribute access.
-
If an attribute’s class defines
__get__,__set__, or__delete__, it’s a descriptor. -
propertyis a built-in descriptor. -
Use cases: validation, type checking, lazy attributes, computed properties, ORM fields etc.
Example:
def __get__(self, instance, owner):
return instance.__dict__[self.name]
def __set__(self, instance, value):
if not isinstance(value, int):
raise TypeError(“Expected int”)
instance.__dict__[self.name] = value
class Point:
x = Integer(‘x’)
y = Integer(‘y’)
p = Point()
p.x = 5
print(p.x)
p.x = “hello” # TypeError
63. How do you implement retry logic (with exponential backoff) in Python?
Answer:
You can write a decorator or helper function to wrap a function invocation with retry logic, optionally with exponential backoff and jitter.
Example:
def retry(max_attempts=3, backoff_factor=0.5, jitter=0.1):
def decorator(fn):
)
def wrapper(*args, **kwargs):
attempt = 1
while True:
try:
return fn(*args, **kwargs)
except Exception as e:
if attempt >= max_attempts:
raise
sleep = backoff_factor * (2 ** (attempt – 1))
sleep = sleep + random.uniform(0, jitter)
time.sleep(sleep)
attempt += 1
return wrapper
return decorator
)
def unreliable_action():
# might throw
…
64. How would you implement a thread-safe queue/stack (or other data structure) in Python?
Answer:
You can use synchronization primitives from threading like Lock, RLock, Condition or use built-in thread-safe data structures:
-
queue.Queue,queue.LifoQueue,queue.PriorityQueueare thread-safe -
If implementing custom, wrap internal data structure operations (push/pop) inside locks:
65. Explain how Python’s bisect module works and give use cases.
Answer:
The bisect module provides functions to insert and search sorted lists, maintaining order in efficient time (binary search). Key functions:
-
bisect_left(a, x): find leftmost insertion point -
bisect_right(a, x)orbisect(a, x): find rightmost insertion point -
insort_left,insort_right: insert into list preserving sorted order
Use cases: maintaining sorted lists, scheduling, prediction, binary searches for thresholds.
66. Write a function to compute the longest increasing subsequence (LIS) of a list of numbers.
Answer (dynamic programming / patience sort):
Simplest (O(n²)):
print(lis([10,9,2,5,3,7,101,18])) # e.g. [2,3,7,18]
Alternatively, O(n log n) algorithms exist using patience sorting technique.
67. How would you implement a publish-subscribe (pub/sub) system in Python?
Answer (simple in-memory version):
For production, you’d use message brokers (RabbitMQ, Kafka), event loops, durable storage, topic filtering, etc.
68. What is method overloading / operator overloading in Python? How can you overload operators?
Answer:
Python doesn’t support method overloading by signature, but you can support different argument types in a single method (via *args, **kwargs) or use functools.singledispatch.
Operator overloading is done by defining “magic” methods in classes:
-
__add__,__sub__,__mul__,__eq__, etc. -
__radd__,__iadd__for reversed or in-place operations
Example:
def __add__(self, other):
return Vec(self.x + other.x, self.y + other.y)
def __repr__(self):
return f”Vec({self.x}, {self.y})”
print(Vec(1,2) + Vec(3,4)) # Vec(4,6)
69. Suppose you have a large log file too big to load entirely in memory. How would you count the most frequent ‘error codes’ in it?
Answer:
Approach:
-
Read line by line (streaming)
-
Use a
collections.Counterordefaultdict(int)to tally counts -
Optionally, use a fixed-size heap to track top-k frequent
-
If the dataset is too large to keep entire counts in memory, you can use:
- Count-Min Sketch (approximate counters)
- Streaming algorithms (e.g. heavy hitters)
- Partition the file (map-reduce style)
Example:
70. How would you implement dependency injection in Python?
Answer:
Dependency injection (DI) means supplying components with their dependencies from the outside rather than creating internally. In Python, you can use:
-
Constructor injection: pass dependencies to
__init__ -
Setter injection: set dependencies via setter methods
-
Use function parameters
-
Use a DI container / framework (e.g.
injector,dependency_injector) -
Use factories or provider functions
Example:
class Service:
def __init__(self, db: DBClient):
self.db = db
# At wiring time:
db = DBClient()
svc = Service(db)
This makes testing easier (you can inject mocks).
71. Explain how you’d design a task scheduler (cron-like) in Python.
Answer (high-level):
Components:
-
Job registry (tasks, schedules, metadata)
-
Scheduler loop / event loop
-
Mechanism to compute next run times (cron syntax or fixed intervals)
-
Worker execution (spawn threads, processes, or background tasks)
-
Persistence (jobs survive restarts)
-
Handling failures, retries, logging
-
Concurrency control, job locking
-
Time zones, daylight savings, drift
-
API to add/remove tasks
You might use or be inspired by existing libraries like APScheduler, Celery beat, or croniter.
72. What are memoryviews and how can they help you optimize Python programs?
Answer:memoryview is a built-in type that provides a “view” of the underlying buffer (e.g. bytes, bytearray, array) without copying data. You can manipulate slices and sub-buffers efficiently.
Use cases:
-
Avoid data copies when slicing large buffers
-
Work with binary data in networking or I/O
-
Zero-copy protocols
Example:
73. How can you dynamically load a module or class by name (i.e. string) at runtime?
Answer:
You can use:
-
importlib.import_module(module_name) -
getattr(module, class_name) -
Or
__import__+getattr
Example:
This is useful for plugin architectures, dependency injection, configuration-driven loading.
74. Explain what monkey patching is and a scenario where it's useful (but also its dangers).
Answer:
Monkey patching is dynamically modifying or overriding classes or modules at runtime.
Use-case:
-
In tests, you override methods or functions in third-party modules to simulate behavior
-
Hotfixing behavior without modifying installed library
Danger:
-
Can lead to unpredictable behavior or maintenance difficulty
-
Hidden side effects
-
Breaks assumptions, especially if library updates
Example:
(You must ensure the new function signature matches expectations.)
75. Suppose you have a stream of data and you want to compute a running median (i.e. median of values seen so far). How would you implement that efficiently?
Answer:
You can maintain two heaps:
-
A max-heap for the lower half
-
A min-heap for the upper half
Balance them so that sizes differ at most by 1. The median is either the top of one heap (if odd count) or average of tops (if even count).
Example sketch:
This gives O(log n) per insertion and constant median retrieval.
Tips / Focus Areas for Accenture Python Interview
-
Be comfortable with data structures & algorithms (lists, sets, dicts, graphs, trees)
-
Practice coding patterns / problem-solving (two-sum, sliding window, merges, dynamic programming)
-
Know standard libraries well:
itertools,collections,functools,heapq,bisect -
Be able to optimize / analyze complexity
-
Be ready to talk about real projects: how you structured code, used Python packages, scaled systems
-
Practice writing clean, readable, testable code (Accenture emphasizes maintainability)
-
Understand concurrency, memory management, profiling