The Get Maximum in Generated Array interview question gives you an integer . You need to generate an array nums of length based on specific rules:
nums[0] = 0nums[1] = 1nums[2 * i] = nums[i] (for )nums[2 * i + 1] = nums[i] + nums[i + 1] (for )
After generating the array, you must return the maximum value it contains.Companies like Meta and Amazon ask this Array simulation problem to test basic coding skills and attention to detail. It’s an "Easy" difficulty question that evaluates your ability to translate mathematical formulas into a loop. It also checks if you can handle edge cases like or correctly without causing an "Index Out of Bounds" error.
This problem is a direct Simulation.
nums[0] = 0 and nums[1] = 1.nums[2*i] and nums[2*i + 1]. Be careful not to write past index .Suppose . Array size is 8.
nums[0] = 0, nums[1] = 1.nums[2] = nums[1] = 1. nums[3] = nums[1] + nums[2] = 1 + 1 = 2.nums[4] = nums[2] = 1. nums[5] = nums[2] + nums[3] = 1 + 2 = 3.nums[6] = nums[3] = 2. nums[7] = nums[3] + nums[4] = 2 + 1 = 3.
Array: [0, 1, 1, 2, 1, 3, 2, 3].
Maximum value is 3.2*i + 1 might attempt to write to index , which doesn't exist.nums[1] = 1 on an array of size 1.When implementing formulas that calculate multiple future indices (like 2*i and 2*i+1), always use if statements to ensure you don't exceed the target array size .