PracHub
QuestionsCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Compute sum over consecutive-step subarrays

Last updated: May 3, 2026

Quick Overview

This question evaluates proficiency in array processing, detection of consecutive-step arithmetic sequences, and accumulation of subarray sums with attention to large-integer (64-bit) arithmetic.

  • medium
  • Google
  • Coding & Algorithms
  • Machine Learning Engineer

Compute sum over consecutive-step subarrays

Company: Google

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Given an integer array `a` of length `n`, call a subarray `a[l..r]` **good** if either: - it is strictly increasing by 1 at every step: `a[i+1] - a[i] = 1` for all `i in [l, r-1]`, **or** - it is strictly decreasing by 1 at every step: `a[i+1] - a[i] = -1` for all `i in [l, r-1]`. (All length-1 subarrays are good.) Define the value of a subarray as the **sum of its elements**. Return the **sum of values of all good subarrays**. ### Requirements - Time complexity: **O(n)** - Use 64-bit integer arithmetic for the result. ### Example Input: `a = [3, 5, 6, 7, 6]` Good subarrays are: - `[3]`, `[5]`, `[6]`, `[7]`, `[6]` - `[5, 6]`, `[6, 7]`, `[5, 6, 7]`, `[7, 6]` Output: `82` ### Constraints (you may assume) - `1 <= n <= 2 * 10^5` - `|a[i]| <= 10^9`

Quick Answer: This question evaluates proficiency in array processing, detection of consecutive-step arithmetic sequences, and accumulation of subarray sums with attention to large-integer (64-bit) arithmetic.

Related Interview Questions

  • Busiest Rental Car - Google (easy)
  • Find Common Free Time Slots Across Calendars - Google (easy)
  • Deterministic Task Execution Order - Google (easy)
  • Count Clusters of 2D Points Within a Radius - Google (medium)
  • Infection Spread on a Grid (Cellular Automaton) - Google (hard)
|Home/Coding & Algorithms/Google

Compute sum over consecutive-step subarrays

Google logo
Google
Feb 8, 2026, 12:00 AM
mediumMachine Learning EngineerOnsiteCoding & Algorithms
9
0
Practice Read
Loading...

Given an integer array a of length n, call a subarray a[l..r] good if either:

  • it is strictly increasing by 1 at every step: a[i+1] - a[i] = 1 for all i in [l, r-1] , or
  • it is strictly decreasing by 1 at every step: a[i+1] - a[i] = -1 for all i in [l, r-1] .

(All length-1 subarrays are good.)

Define the value of a subarray as the sum of its elements. Return the sum of values of all good subarrays.

Requirements

  • Time complexity: O(n)
  • Use 64-bit integer arithmetic for the result.

Example

Input: a = [3, 5, 6, 7, 6]

Good subarrays are:

  • [3] , [5] , [6] , [7] , [6]
  • [5, 6] , [6, 7] , [5, 6, 7] , [7, 6]

Output: 82

Constraints (you may assume)

  • 1 <= n <= 2 * 10^5
  • |a[i]| <= 10^9

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Machine Learning Engineer•Google Machine Learning Engineer•Google Coding & Algorithms•Machine Learning Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.