Classify a Number Series and Return the Next Term
Updated
Problem statement
Given an integer array sequence, decide whether it follows an arithmetic progression, an integer-ratio geometric progression, or a Fibonacci-style progression. Return the next term. If it follows none of them, return -999.
Classification Rules
- First test for an arithmetic progression: every consecutive difference must be equal.
- If that test fails, test for a geometric progression: there must be one integer multiplier
rsuch thatsequence[i] = sequence[i - 1] * rfor every validi. - If both earlier tests fail, test for a Fibonacci-style progression:
sequence[i] = sequence[i - 1] + sequence[i - 2]for everyi >= 2. The first two terms may be any integers.
Run the tests in the order above. If a sequence fits more than one rule, the first rule that fits decides the answer.
Function
nextSeriesNumber(sequence: long[]) → longExamples
Example 1
sequence = [2,5,8,11]return = 14The consecutive difference is always 3, so this is an arithmetic progression and the next term is 11 + 3 = 14.
Example 2
sequence = [3,6,12,24]return = 48Each term is the previous term multiplied by 2, so the next term is 48.
Example 3
sequence = [8,9,17,26]return = 43Each term from the third onward is the sum of the previous two, so the next term is 17 + 26 = 43.
Example 4
sequence = [1,2,4,7]return = -999The sequence satisfies none of the three progression rules, so the sentinel -999 is returned.
Constraints
3 <= sequence.length <= 1000-10^9 <= sequence[i] <= 10^9- Every multiplication, addition, difference, and valid next term fits in a signed
long.
More InMobi Online Assessment questions
- Validate a Bracket SequenceOA · Seen Oct 2026
- Count Substrings Matching with One Adjacent SwapOA · Seen Oct 2026
- Maximum Subarray with Start and End WindowsOA · Seen Oct 2026
- Minimum Service Cold StartsOA · Seen Oct 2026
- Range Maximum Subarray Sum QueriesOA · Seen Oct 2026
- Connected Components with Divisor ThresholdOA · Seen Aug 2026
- Maximize Impression ValueOA · Seen Aug 2026
- Supply Path OptimizationOA · Seen Aug 2026
- Delivery ServiceOA · Seen Jun 2026
- Shared InterestOA · Seen Jun 2026
Similar questions at other companies
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Costliest ChocolateRippling · OARippling interview questions
- Online No-Intercept Linear RegressionTwo Sigma · OATwo Sigma interview questions
- Plus Mult ArrayIBM · OAIBM interview questions
- Progressive Tax CalculatorInteractive Brokers · OA
- 2020 Account Balance with Monthly Card FeesDRW · OADRW interview questions
- Array Challenge (QR Intern)Akuna Capital · OAAkuna Capital interview questions
- Latency Bucket CounterDatadog · OA · PHONE SCREENDatadog interview questions
- First Fibonacci Number at Least the Array SumBitGo · OABitGo interview questions