




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































Question 1. Which of the following best describes the âdivide and conquerâ paradigm? A) Trying every possible solution until one works B) Breaking a problem into overlapping subâproblems and reusing results C) Splitting a problem into independent subâproblems, solving each recursively, then combining the results D) Selecting the locally optimal choice at each step hoping to reach a global optimum Answer: C Explanation: Divide and conquer divides a problem into independent parts, solves each recursively, and merges the solutions (e.g., merge sort). Overlapping subâproblems belong to dynamic programming, while brute force tries all possibilities, and greedy picks locally optimal choices.
Question 2. In a backtracking algorithm, which technique is used to avoid exploring the same state multiple times? A) Memoization B) Pruning C) Greedy selection D) Topological sorting Answer: B Explanation: Pruning removes branches of the search tree that cannot lead to a valid solution, reducing the stateâspace explored. Memoization stores results of subâproblems, but pruning directly cuts off unnecessary paths.
Question 3. What is the worstâcase time complexity of binary search on a sorted array of size n? A) O(n)
B) O(log n) C) O(n log n) D) O(1) Answer: B Explanation: Binary search repeatedly halves the search interval, leading to at most âlogâ nâ comparisons.
Question 4. Which greedy algorithm solves the fractional knapsack problem optimally? A) Choose items in order of increasing weight B) Choose items in order of decreasing value C) Choose items in order of decreasing valueâtoâweight ratio D) Choose items randomly until the knapsack is full Answer: C Explanation: For the fractional knapsack, taking items with the highest valueâtoâweight ratio first yields the optimal solution because fractions of items are allowed.
Question 5. In dynamic programming, what is the main difference between memoization and tabulation? A) Memoization uses bottomâup iteration, tabulation uses recursion B) Memoization stores results in a hash map, tabulation uses an array C) Memoization is topâdown with caching, tabulation is bottomâup filling a table iteratively D) Memoization requires O(1) space, tabulation requires O(n) space Answer: C
Question 8. Which graph traversal algorithm is guaranteed to find the shortest path (in terms of number of edges) in an unweighted graph? A) DepthâFirst Search (DFS) B) BreadthâFirst Search (BFS) C) Dijkstraâs algorithm D) BellmanâFord algorithm Answer: B Explanation: BFS explores vertices level by level, ensuring the first time a vertex is reached it is via the shortestâedge path.
Question 9. In Dijkstraâs algorithm, why must edge weights be nonânegative? A) Negative weights cause infinite loops in the priority queue B) The algorithm assumes that once a vertex is extracted, its distance is final, which fails with negative edges C) The algorithm cannot handle fractional weights D) Negative weights are not allowed in any graph algorithm Answer: B Explanation: Dijkstraâs relies on the property that a vertexâs shortest distance is settled when extracted. Negative edges can later produce a shorter path, violating this invariant.
Question 10. Which of the following statements about the BellmanâFord algorithm is true? A) It works only on directed acyclic graphs
B) It can detect negative weight cycles reachable from the source C) Its time complexity is O(V + E) D) It always runs faster than Dijkstraâs algorithm Answer: B Explanation: BellmanâFord relaxes edges Vâ1 times and then checks for further relaxation to detect negative cycles reachable from the source.
Question 11. Kruskalâs algorithm for Minimum Spanning Tree uses which data structure to efficiently detect cycles? A) Stack B) Queue C) Disjoint Set Union (DSU) D) Binary Search Tree Answer: C Explanation: DSU (UnionâFind) maintains components and quickly checks whether adding an edge would connect two vertices already in the same set, indicating a cycle.
Question 12. In a flow network, the value of the maximum flow equals the capacity of the minimum cut according to which theorem? A) FordâFulkerson theorem B) Maxâflow minâcut theorem C) Hallâs marriage theorem D) KĂśnigâs theorem
Question 15. Which hashâcollision resolution technique guarantees O(1) worstâcase lookup time? A) Separate chaining with linked lists B) Open addressing with linear probing C) Cuckoo hashing with two hash functions D) Quadratic probing Answer: C Explanation: Cuckoo hashing ensures that each key resides in one of two possible buckets; reâhashing occurs when a cycle is detected, giving O(1) worstâcase lookup.
Question 16. In a segment tree, what is the time complexity to perform a range sum query and a point update? A) O(log n) for both B) O(n) for query, O(log n) for update C) O(log n) for query, O(n) for update D) O(1) for both Answer: A Explanation: Both operations traverse the height of the balanced binary tree, which is O(log n).
Question 17. Which numberâtheoretic algorithm computes all primes up to N in O(N log log N) time? A) Trial division
B) Sieve of Eratosthenes C) MillerâRabin primality test D) Euclidean algorithm Answer: B Explanation: The Sieve of Eratosthenes marks multiples of each prime, achieving O(N log log N) complexity.
Question 18. When using modular exponentiation to compute aâż mod m, which method achieves O(log n) time? A) Repeated multiplication B) Fast exponentiation (binary exponentiation) C) Euclidean algorithm D) Chinese Remainder Theorem Answer: B Explanation: Binary exponentiation repeatedly squares the base and multiplies when the current exponent bit is 1, giving logarithmic steps.
Question 19. The Pigeonhole Principle states that if n items are placed into m containers and n > m, then: A) At least one container will be empty B) At least one container will contain more than one item C) All containers will contain exactly one item D) The distribution is impossible Answer: B
Question 22. Matrix exponentiation can be used to compute the nth Fibonacci number in O(log n) time because: A) Fibonacci numbers are linear recurrences that can be expressed as a matrix power B) It reduces the problem to integer multiplication C) It avoids recursion entirely D) It uses the closedâform Binet formula implicitly Answer: A Explanation: The recurrence Fâ = Fâââ + Fâââ can be written as a 2Ă2 matrix; raising the matrix to the (nâ1)th power yields Fâ, and fast exponentiation gives O(log n).
Question 23. Which of the following best illustrates âtechnical debtâ in a codebase? A) Using a wellâdocumented library instead of writing custom code B) Adding a quick hack that works now but will require extensive refactoring later C) Writing unit tests for every function D) Choosing an algorithm with optimal asymptotic complexity Answer: B Explanation: Technical debt refers to shortcuts (e.g., hacks) that speed up delivery now but increase future maintenance cost.
Question 24. When is a âclever oneâlinerâ more likely to be a liability than an asset? A) When it reduces the number of loops from two to one B) When it uses a language feature that is wellâknown to all team members
C) When it sacrifices readability, making future debugging hard D) When it improves the algorithmic complexity from O(n²) to O(n log n) Answer: C Explanation: Obscure oneâliners hinder readability and maintainability, outweighing any brevity benefits.
Question 25. Which C++ STL container provides amortized O(1) insertion at the end and O(1) random access? A) std::list B) std::deque C) std::vector D) std::forward_list Answer: C Explanation: std::vector stores elements contiguously, allowing constantâtime random access and amortized constant insertion at the back.
Question 26. In Python, which itertools function generates the Cartesian product of input iterables? A) permutations() B) combinations() C) product() D) chain() Answer: C Explanation: itertools.product(*iterables) yields the Cartesian product.
A) Using std::cin and std::cout with sync_with_stdio(false) and cin.tie(nullptr) B) Using scanf/printf instead of iostreams C) Writing data to a file and reading it back later D) Increasing the stack size Answer: A Explanation: Disabling sync with stdio and untie-ing cin from cout makes iostreams as fast as C I/O functions.
Question 30. Which of the following statements about recursion depth in Python is true? A) Python has no recursion limit B) The default recursion limit is 1000, and exceeding it raises a RecursionError C) Recursion depth is limited only by available memory D) Tailâcall optimization eliminates the recursion limit automatically Answer: B Explanation: Python sets a default recursion limit (usually 1000). Exceeding it raises RecursionError; Python does not perform tailâcall optimization.
Question 31. In a topâdown DP implementation with memoization, which data structure is most commonly used to store cached results for subâproblems defined by two integer parameters (i, j)? A) 1âdimensional array B) 2âdimensional array or vector of vectors C) Hash map with string keys âi#jâ D) Stack
Answer: B Explanation: When subâproblems are indexed by two integers, a 2âD table provides O(1) access to cached values.
Question 32. Which of the following graph representations is most spaceâefficient for a sparse graph with V vertices and E edges (E ⪠V²)? A) Adjacency matrix B) Adjacency list using vectors of pairs C) Incidence matrix D) Edge list without additional structure Answer: B Explanation: An adjacency list stores only existing edges, using O(V + E) space, ideal for sparse graphs.
Question 33. When applying the âunionâbyârankâ heuristic in DSU, what does ârankâ represent? A) The size of the set B) The depth of the tree representing the set C) The number of times the set has been merged D) The hash code of the setâs representative Answer: B Explanation: Rank approximates the tree depth; merging the lowerârank tree under the higherârank one keeps the overall tree shallow.
A) Sorting an array of integers B) Detecting duplicate substrings in a long text (RabinâKarp) C) Encrypting data securely D) Generating random numbers Answer: B Explanation: Rolling hashes allow O(1) hash updates when sliding a window, enabling efficient substring matching as in the RabinâKarp algorithm.
Question 37. When solving a problem with âdigit DPâ, what is the typical state component? A) Current index in the array and remaining sum B) Current digit position, tight flag (whether prefix matches limit), and possibly other constraints C) Number of edges processed so far D) Height of a binary tree processed so far Answer: B Explanation: Digit DP tracks the position in the number, whether the constructed prefix is already less than the bound (tight flag), and any extra constraints like digit sum.
Question 38. Which of the following is a property of a balanced binary search tree that guarantees O(log n) search time? A) The tree is complete (all levels fully filled) B) The height of the tree is Î(log n) C) All leaf nodes are at the same depth
D) The tree stores keys in a hash table internally Answer: B Explanation: Maintaining height proportional to log n ensures search, insertion, and deletion each take logarithmic time.
Question 39. In the context of formal verification, what does âvacuous solutionâ refer to? A) A proof that assumes false premises, thus trivially true B) An algorithm that runs in constant time C) A test case that covers no input D) A program that never terminates Answer: A Explanation: A vacuous solution satisfies a specification because the precondition is never met (i.e., the statement is trivially true), which is undesirable in verification.
Question 40. Which proof assistant is commonly used to formally verify functional correctness of code written in a functional language? A) Git B) Lean C) Docker D) JUnit Answer: B Explanation: Lean is a proof assistant designed for formalizing mathematics and verifying program properties.
Question 43. Which of the following best describes âpath compressionâ in DSU? A) Storing the entire path as a string for debugging B) During Find, making each visited node point directly to the root, flattening the tree C) Merging two sets by attaching the smaller tree under the larger one D) Keeping a list of all elements in each set for quick enumeration Answer: B Explanation: Path compression updates parent pointers during Find, ensuring future finds are faster.
Question 44. Which algorithmic technique is most suitable for solving the âsubset sumâ problem when the target sum S is relatively small (⤠10âľ) but the number of items N is large (â 10â´)? A) Bruteâforce enumeration of all subsets B) Greedy approximation C) DP over the sum dimension (O(N¡S)) D) Binary search on sorted items Answer: C Explanation: When S is modest, DP over possible sums (knapsack style) runs in O(N¡S) and is feasible, unlike exponential enumeration.
Question 45. In a lazy segment tree, what is the purpose of âlazy propagationâ? A) To delay building the tree until the first query B) To store pending updates at internal nodes and apply them only when needed, reducing update time C) To compress the tree into a smaller memory footprint
D) To convert the tree into a binary indexed tree automatically Answer: B Explanation: Lazy propagation records updates without immediately traversing all affected leaves, applying them later during queries or further updates.
Question 46. Which of the following is a correct statement about the time complexity of the quicksort algorithm on average? A) O(n²) B) O(n log n) C) O(log n) D) O(n) Answer: B Explanation: Averageâcase quicksort partitions the array recursively, leading to O(n log n) expected time; worst case can degrade to O(n²).
Question 47. When using the âtwoâpointerâ technique on a sorted array to find a pair that sums to a target T, what is the overall time complexity? A) O(n²) B) O(n log n) C) O(n) D) O(log n) Answer: C