FastPrepCalculate Region
Problems/Calculate Region

Calculate Region

The D.E. Shaw Group logoThe D.E. Shaw Group● MediumOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

There is a straight line of students of various heights. The students' heights are given in the form of an array, in the order they are standing in the line.

Consider the region of a student as the length of the largest subarray that includes that student's position, and in which that student's height is equal to maximum height among all students present in that subarray. Return the sum of the region of all students.

Function

calculateTotalRegion(heights: int[]) → long

Complete the function calculateTotalRegion in the editor.

calculateTotalRegion has the following parameter(s):

  • heights: an array of the heights of students standing in the line

Returns long integer: the desired sum of all regions

Examples

Example 1

heights = [3, 5, 6]return = 6

The longest subarray in which the first student's height is equal to the maximum height among all other students is [3]; thus, the length of the region of the first student is 1. The longest subarray in which the second student's height is equal to maximum height among all other students is [3, 5]; thus, the length of the region of the second student is 2. The longest subarray in which the third student's height is equal to the maximum height among all other students is [3, 5, 6]; thus, the length of the region of the third student is 3. Thus, the sum of the lengths of all regions of all students is 1 + 2 + 3 = 6 (But the ans on the source img is 5, which is quite confusing...Many thanks in advance if you could help clarify the calculation 👍).

Example 2

heights = [1, 2, 1]return = 5

Subarrays for values 1, 2, and 1 are [1], [1, 2], and [1]. The sum of their lengths is 1 + 3 + 1 = 5.

Constraints

  • 1 ≤ length of heights ≤ 105
  • 1 ≤ heights[i] ≤ 109

More The D.E. Shaw Group interview questions

  • IPOPHONE SCREEN · Seen Nov 2026
  • Print Numbers in Turn with Three ThreadsPHONE SCREEN · Seen Nov 2026
  • Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
  • Koko Eating BananasPHONE SCREEN · Seen Nov 2026
  • Sum of Smallest Non-DivisorsPHONE SCREEN · Seen Nov 2026
  • Count Lucky TicketsOA · Seen Nov 2026
  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Coordinate Odd and Even Workers in OrderONSITE INTERVIEW · Seen Oct 2026
  • Minimum Taps to Water a GardenONSITE INTERVIEW · Seen Oct 2026
  • Remove the Fewest Magic BeansPHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Sum of Subarray MinimumsNVIDIA · OANVIDIA interview questions
  • Shopkeeper Final Price SummaryMicrosoft · OAMicrosoft interview questions
  • Nested Recipe Blocks (Game Adaptation)Roblox · OARoblox interview questions
  • Maximum Capped Histogram Area After One RemovalMyntra · OAMyntra interview questions
  • Steps to the Next Higher Value in a CircleRippling · OARippling interview questions
  • Shopkeeper Final PriceUber · OAUber interview questions
  • Predict AnswerAmazon · OA
  • Retention Policy CollisionBox · OABox interview questions
  • Longest Prefix-Valid Binary SubarrayHackerRank · OAHackerRank interview questions
All The D.E. Shaw Group interview questionsSee The D.E. Shaw Group hiring insights
Saved
class Solution {
    public long calculateTotalRegion(int[] heights) {
      // write your code here
    }

}
Checking account…