This is a verified interview question from Amazon-ml-summer-school. Candidates reporting seeing this problem in recent Online Assessments (OAs) and onsite rounds. Mastering "Streetlight Motion Detection Duration - AMAZON ML SUMMER SCHOOL" covers key patterns like Arrays.
"### Problem In a city, a streetlight turns on automatically and stays on for exactly B seconds each time it detects motion. If it detects motion again before turning off, the timer resets, and it stays on for another B seconds from the new detection time. You have a list A where each element A[i] indicates the time when the streetlight detects motion, and an integer B. Calculate the total time the streetlight remains on. ### Input - First Argument is an Integer Array, A denoting the time when the streetlight detects motion. - Second Argument is an Integer, B denoting the duration in seconds. ### Output Return an Integer, denoting the total time the streetlight remains on. ### Constraints - 1 <= A.length <= 10^5 - 0 <= A[i], B <= 10^9 - A is in non-decreasing order ### Example Input Input 1: A = [1, 4] B = 2 Input 2: A = [1, 2] B = 2 ### Example Output Output 1: 4 Output 2: 3 ### Example Explanation Explanation 1: At second 1, the streetlight detects motion and turns on for seconds 1 and 2. At second 4, the streetlight detects motion again and turns on for seconds 4 and 5. The streetlight is on for seconds 1, 2, 4, and 5, which is 4 seconds in total. Explanation 2: At second 1, the streetlight detects motion and turns on for seconds 1 and 2. At second 2, the streetlight detects motion again and resets the timer. It stays on for seconds 2 and 3. The streetlight is on for seconds 1, 2, and 3, which is 3 seconds in total."
Join thousands of developers practicing for Amazon-ml-summer-school.