Docsity

Clever Coding Ultimate Exam, Exams of Technology

Toggle sidebar
bg1
Clever Coding Ultimate Exam
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)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Clever Coding Ultimate Exam and more Exams Technology in PDF only on Docsity!

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