Loading samples...
Loading samples...
py-sample-18
Mixed topics: Data structures, algorithms, best practices
What is time complexity of list.append()?
Which data structure for fast lookups?
What is list comprehension syntax?
Which is NOT a Python sequence?
What does zip() return in Python 3?
What is the default recursion limit?
Which sorts in-place?
What is a generator function?
Which keyword creates generator?
What is time complexity of dict lookup?
Explain the difference between list and generator comprehension.
[3 marks]What is the difference between deep copy and shallow copy?
[3 marks]Explain the map(), filter(), and reduce() functions.
[3 marks]What are Python's built-in data structures? Compare them.
[3 marks]Explain the any() and all() functions.
[3 marks]What is memoization? How to implement it?
[3 marks]Explain itertools module and common functions.
[3 marks]What is the difference between sort() and sorted()?
[3 marks]Explain the collections module: Counter, defaultdict, OrderedDict.
[3 marks]What are Python's time complexities for common operations?
[3 marks]Implement binary search on a sorted list.
[5 marks]Create a custom queue class using collections.deque.
[5 marks]Write a function to flatten nested lists of any depth.
[5 marks]Implement LRU cache using OrderedDict or dict + linked list concept.
[5 marks]Write a function to find all permutations of a string.
[5 marks]Create a priority queue implementation using heapq module.
[5 marks]Predict output: squares = [x**2 for x in range(5)] print(squares)
[2 marks]Explanation:List comprehension squares each number 0-4. Creates new list with results.
Find error: gen = (x**2 for x in range(3)) print(len(gen))
[2 marks]Explanation:Generators don't support len(). They produce values on demand and don't store them all.
Predict output: from functools import reduce result = reduce(lambda a, b: a * b, [1, 2, 3, 4]) print(result)
[2 marks]Explanation:reduce applies function cumulatively: ((1*2)*3)*4 = 24. Product of all elements.
Find error: def infinite(): n = 0 while True: yield n n += 1 list(infinite())
[3 marks]Explanation:Trying to convert infinite generator to list never ends. Use islice() or take first N with next() in loop.
Predict output: a = [1, 2, 3] b = a.copy() b.append(4) print(a, b)
[2 marks]Explanation:copy() creates shallow copy. a and b are independent lists. Modifying b doesn't affect a.
Find error: import copy a = [[1], [2], [3]] b = copy.copy(a) b[0][0] = 99 print(a)
[3 marks]Explanation:shallow copy copies outer list but nested lists are still references. Use deepcopy() for full independence.
Predict output: from collections import Counter words = ['apple', 'banana', 'apple', 'cherry', 'banana', 'apple'] c = Counter(words) print(c.most_common(2))
[3 marks]Explanation:Counter counts occurrences. most_common(2) returns top 2 items sorted by count descending.
Find error: items = [3, 1, 4, 1, 5] items.sort().reverse() print(items)
[2 marks]Explanation:sort() returns None (in-place). Can't chain methods that return None. Use items.sort() then items.reverse().