L3 google onsite Arithmetic sequence question.
Summary
I interviewed for a L3 role at Google and was rejected after failing the arithmetic sequence coding round.
Full Experience
Hi everyone, I recently completed my google loop, unfortunatly i was rejected because i bombed the round where i was asked this question, i panicked and managed only a brute force with a couple of hints.
An arithmetic sequence is a list of numbers with a definite pattern. If you take any number in the sequence then subtract it from the previous one, the difference is always a constant.
A good arithmetic sequence is an arithmetic sequence with a common difference of either 1 or -1.
For example, [4, 5, 6] is a good arithmetic sequence. Any sequence that has only one element is a good arithmetic sequence.
For example, [4] is a good arithmetic sequence.
Given an integer array nums, return the sum of the sums of each subarray that is a good arithmetic sequence.
Example:
Given nums = [7, 4, 5, 6, 5]. Each of the following subarrays is a good arithmetic sequence:
- [7], [4], [5], [6], [5],
- [4, 5], [5, 6], [6, 5],
- [4, 5, 6]
The sums of these subarrays are:
- 7, 4, 5, 6, 5,
- 4 + 5 = 9, 5 + 6 = 11, 6 + 5 = 11,
- 4 + 5 + 6 = 15
Thus, the answer is the sum of all the sums above, which is:
7 + 4 + 5 + 6 + 5 + 9 + 11 + 11 + 15 = 73.
Now I know that we can find the start and end of each sequence and calculate how many segments each number appears in. Unfortunatly I didn't know that pattern and math formula before this interview...
Interview Questions (1)
Sum of Sums of Good Arithmetic Subarrays
Given an integer array nums, return the sum of the sums of each subarray that is a good arithmetic sequence.
A good arithmetic sequence is an arithmetic sequence whose common difference is either 1 or -1. Any single‑element subarray is also considered a good arithmetic sequence.
Example
Input: nums = [7, 4, 5, 6, 5]
Good arithmetic subarrays:
- [7], [4], [5], [6], [5]
- [4, 5], [5, 6], [6, 5]
- [4, 5, 6]
Their sums are:
- 7, 4, 5, 6, 5
- 4+5 = 9, 5+6 = 11, 6+5 = 11
- 4+5+6 = 15
Result = 7 + 4 + 5 + 6 + 5 + 9 + 11 + 11 + 15 = 73.