FastPrepCount Sawtooth Subarrays
Problems/Count Sawtooth Subarrays

Count Sawtooth Subarrays

Hudson River Trading logoHudson River Trading● EasyFULLTIMENEW GRADINTERNOA

Updated Nov 2026

QuestionFunctionExamples

Problem statement

Source note: Reported in a Hudson River Trading online assessment. The report does not give the full limits on the numbers. Our version is about 97% the same as the reported question.

A sawtooth sequence alternates between even and odd values: every adjacent pair contains one even value and one odd value.

Given an integer array arr, count the number of contiguous subarrays that are sawtooth sequences. Every non-empty subarray is eligible, and a single-element subarray is always valid.

Function

countSawtoothSubarrays(arr: int[]) → long

Examples

Example 1

arr = [1,3,5,7,9]return = 5

Every pair of adjacent values is odd-odd, so no subarray of length at least 2 is valid. The five single-element subarrays are valid, so the result is 5.

Example 2

arr = [1,2,1,2,1]return = 15

The entire array alternates parity, so all 5 * 6 / 2 = 15 contiguous subarrays are valid.

Example 3

arr = [1,2,3,7,6,5]return = 12

The only parity break is the adjacent odd pair 3, 7. The alternating runs are [1, 2, 3] and [7, 6, 5]; each run contributes 3 * 4 / 2 = 6 valid subarrays, for a total of 12.

More Hudson River Trading Online Assessment questions

  • Count Palindrome Addition ChoicesOA · Seen Nov 2026
  • Count Multiples of Three with Two SevensOA · Seen Oct 2026
  • Recursively Expanded Shell Command CountsOA · Seen Oct 2026
  • Rotate Matrix Regions Around the DiagonalsOA · Seen Oct 2026
  • Maximum Diamond SumOA · Seen Oct 2026
  • Smallest Most-Lit Street PositionOA · Seen Oct 2026
  • Rearrange String by Alternating EndsOA · Seen Sep 2026
  • Most Items Within Budget from a Start IndexOA · Seen Sep 2026
  • Count Fancy NumbersOA · Seen Sep 2026
  • Count Pieces After Othello MovesOA · Seen Sep 2026

Similar questions at other companies

  • Speed Up a Slow FunctionCitadel · OA · PHONE SCREENCitadel interview questions
  • Maximum Income from Asset TradingJefferies · OA
  • Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
  • Maximum Total of Nonadjacent RatingsMorgan Stanley · OAMorgan Stanley interview questions
  • Count Sawtooth SubarraysTikTok · OATikTok interview questions
  • Count Alternating-Parity SubarraysVisa · OAVisa interview questions
  • Find Largest Sum Contiguous SubarrayCisco · OACisco interview questions
  • Maximum Sum of Non-Adjacent ElementsLSEG · OALSEG interview questions
  • Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
  • Maximum Beauty by DeletionsMeesho · OA

Hudson River Trading interview experiences

  • Hudson River Trading Data Science Interviews: 2 Experiences
  • Hudson River Trading interviews · 2021
  • Hudson River Trading OA · 2021
  • Hudson River Trading interviews · 2022
  • Hudson River Trading OA · 2022
  • Hudson River Trading interviews · 2023
All Hudson River Trading interview questionsSee Hudson River Trading hiring insights
Saved
class Solution {
    public long countSawtoothSubarrays(int[] arr) {
        // write your code here
    }

}
Checking account…