InterviewStack.io LogoInterviewStack.io

Python Data Structures and Algorithms Questions

Core Python data structure and algorithm knowledge used for manipulating collections and solving common data processing problems. Candidates should know built in types such as lists, dictionaries, sets, and tuples and their performance characteristics; be able to implement and reason about searching, sorting, counting, deduplication, and frequency analysis tasks; and choose appropriate algorithms and data structures for time and space efficiency. Familiarity with Python standard library utilities such as collections.Counter, defaultdict, deque, and heapq is expected, as is writing Pythonic, clear code that handles edge cases. Questions may include algorithmic trade offs, complexity analysis, and applying these techniques to practical data manipulation problems where custom logic is required beyond what pandas or NumPy provide.

EasyTechnical
0 practiced
Explain how heapq.nlargest and heapq.nsmallest are implemented in Python and when they outperform sorting. State time complexity for the cases k << n and k ~ n and discuss memory trade-offs and stability considerations when extracting top-k elements.
EasyTechnical
0 practiced
Given a Python list of labels (strings) representing predicted classes (for example: ['cat','dog','cat','bird',...]), write code using collections.Counter to return the top 5 most frequent labels and their counts. Explain briefly how Counter.most_common works internally and the approximate time/space complexity for large inputs.
MediumTechnical
0 practiced
Write a Python function flatten_dict(d) that converts a nested dictionary into a single-level dictionary using dot-separated keys and converts numeric strings to ints/floats where possible. Example input: {'user':{'cnt':'5','errors':0}} -> {'user.cnt': 5, 'user.errors': 0}. Handle nested lists by including indices in keys.
MediumTechnical
0 practiced
Implement a RunningMedian class in Python that supports add(num) and get_median() using two heaps (max-heap for lower half, min-heap for upper half) so that each insertion is O(log n) and retrieving the median is O(1). Provide code and explain handling even/odd counts and negative numbers.
MediumTechnical
0 practiced
Given a stream of event records where duplicates are defined by equality on a subset of fields (e.g., user_id and event_type) but timestamps may differ, implement a Python deduplication function that keeps the latest event per key in a single pass and is memory-efficient. Allow passing a custom key function and describe eviction strategies for long-running streams.

Unlock Full Question Bank

Get access to hundreds of Python Data Structures and Algorithms interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.