CodeSignal Technical Assessment Coding Fundamentals 3 — Questions and Answers
Question 1: Which data structure is most efficient for implementing a priority queue?
- Linked List
- Binary Heap (Correct answer)
- Hash Table
- Stack
Correct answer: Binary Heap
A binary heap supports O(log n) insertion and O(log n) extraction of the minimum/maximum, making it ideal for priority queues.
Question 2: What is the output of the following Python code? ``` def f(x, lst=[]): lst.append(x) return lst print(f(1)) print(f(2)) ```
- [1]\n[2]
- [1]\n[1, 2] (Correct answer)
- [1]\n[2, 1]
- Error
Correct answer: [1]\n[1, 2]
Python default mutable arguments are shared across calls, so the same list persists between invocations.
Question 3: In Big-O notation, which represents the fastest growth rate?
- O(n log n)
- O(2ⁿ) (Correct answer)
- O(n²)
- O(n³)
Correct answer: O(2ⁿ)
Exponential time O(2ⁿ) grows faster than polynomial complexities like O(n²) or O(n³) for large n.
Question 4: What is a key difference between a shallow copy and a deep copy of an object?
- Shallow copies are faster to create
- Deep copy duplicates nested objects; shallow copy only copies top-level references (Correct answer)
- Shallow copies work only on primitives
- Deep copies share memory with the original
Correct answer: Deep copy duplicates nested objects; shallow copy only copies top-level references
A shallow copy copies the container but not nested objects, which are still shared; a deep copy recursively copies everything.
Question 5: Which statement about recursion is TRUE?
- Recursive solutions always use less memory than iterative ones
- Every recursive function must have a base case to avoid infinite recursion (Correct answer)
- Recursion cannot be replaced by iteration
- Recursive functions cannot accept parameters
Correct answer: Every recursive function must have a base case to avoid infinite recursion
A base case is required in recursion to stop the recursive calls; without it, the function calls itself indefinitely.
Question 6: What does `O(1)` space complexity mean for an algorithm?
- The algorithm uses no memory at all
- The algorithm's memory usage doesn't grow with input size (Correct answer)
- The algorithm runs in constant time
- The algorithm only uses a single variable
Correct answer: The algorithm's memory usage doesn't grow with input size
O(1) space means the amount of extra memory used remains constant regardless of how large the input is.
Question 7: In a binary search algorithm, what is the prerequisite for the input array?
- The array must have an odd number of elements
- The array must be sorted (Correct answer)
- The array must contain only integers
- The array must have no duplicates
Correct answer: The array must be sorted
Binary search relies on comparing the target to the midpoint and eliminating half the array, which only works on a sorted array.
Which data structure is most efficient for implementing a priority queue?