Question Bank
Array Fundamentals Quiz
Difficulty: Easy
Quick prompts on indexing, in-place mutation, and the cost of common array operations. Good for warming up before sliding-window or two-pointer drills.
Array Fundamentals Quiz
Quick prompts on indexing, in-place mutation, and the cost of common array operations. Good for warming up before sliding-window or two-pointer drills.
979 views
16
What does this snippet print and why? Identify the cost of each line in Big-O.
Examples
Example 1:
Input: a = [10, 20, 30, 40, 50]; read a[2], a.length, push 60, read a[a.length - 1]
Output: 30, then 5, then 60
Explanation: a[2] is an O(1) index read (30). a.length is O(1) cached (5). push appends 60 in amortized O(1), and the new last element is 60.Implement rotateLeft(arr, k) that rotates arr left by k positions in place in O(n) time and O(1) extra space. Assume 0 <= k < arr.length.
Examples
Example 1:
Input: arr = [1, 2, 3, 4, 5], k = 2
Output: [3, 4, 5, 1, 2]
Explanation: The three-reversal trick reverses [0, k), reverses [k, n), then reverses the whole array, rotating in place in O(n) time and O(1) extra space.Example 2:
Input: arr = [1, 2, 3], k = 0
Output: [1, 2, 3]
Explanation: With k = 0 the three reversals cancel out and the array is unchanged.What is the time complexity of arr.splice(0, 1) on an array of length n, and what does arr.shift() do differently?
Spot the bug. The function below should return a new array with each element doubled, but the caller's array is being mutated.
Examples
Example 1:
Input: nums = [1, 2, 3]; result = doubled(nums)
Output (buggy version): nums = [2, 4, 6], result = [2, 4, 6] pointing at the SAME array
Output (fixed version): nums = [1, 2, 3], result = [2, 4, 6] as a fresh array
Explanation: Arrays are passed by reference, so writing arr[i] = ... mutates the caller's array. Using arr.map(x => x * 2) returns a new array and leaves the input untouched.