FastPrepCount Sawtooth Subarrays
Problems/Count Sawtooth Subarrays

Count Sawtooth Subarrays

TikTok logoTikTok● EasyFULLTIMEOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Given an integer array, count the number of contiguous subarrays that form a sawtooth sequence — one where adjacent elements have alternating parity (even and odd alternate throughout the subarray).

A single element is also considered a valid sawtooth sequence.

Approach hint: Traverse the array and maintain the current length cur of the alternating sequence ending at each index. If the current and previous elements have different parity, increment cur; otherwise reset cur = 1. Add cur to the result at each step, since it represents the number of valid subarrays ending at the current index.

Function

countSawtoothSubarrays(nums: int[]) → int

Examples

Example 1

nums = [1,2,3]return = 6

All 6 subarrays are valid sawtooth sequences: [1], [2], [3] (single elements), [1,2] (odd→even), [2,3] (even→odd), [1,2,3] (odd→even→odd). Total = 6.

Example 2

nums = [1,1]return = 2

[1] and [1] are valid (single elements). [1,1] has the same parity (odd, odd) so it is not a sawtooth sequence. Total = 2.

More TikTok interview questions

  • Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
  • Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 2026
  • Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
  • Shell Command Counts with History ReferencesOA · Seen Oct 2026
  • Count Subarrays Matching a Comparison PatternOA · Seen Sep 2026
  • Product Minus Sum of DigitsOA · Seen Aug 2026
  • Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
  • Find All Local PeaksOA · Seen Aug 2026
  • Count Case-Insensitive TripletsOA · Seen Aug 2026

Similar questions at other companies

  • Maximum Income from Asset TradingJefferies · OA
  • Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
  • Count Sawtooth SubarraysHudson River Trading · OAHudson River Trading interview questions
  • Maximum Total of Nonadjacent RatingsMorgan Stanley · OAMorgan Stanley 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
  • 0/1 KnapsackConfluent · OAConfluent interview questions
  • Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
  • Largest Consistent Visiting GroupAuxoAI · OA

TikTok interview experiences

  • TikTok AI Infra Intern LLM Round 2: 1 Experience
  • TikTok AI Infra Intern DeepSpeed Round: 1 Experience
  • TikTok AI Infra Intern Kernel Round: 1 Experience
  • TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
  • TikTok AI Infra Intern Interview Timeline: 1 Experience
  • TikTok AI Infra Intern Onsite Round 2: 1 Experience
All TikTok interview questionsSee TikTok hiring insights
Saved
class Solution {
    public int countSawtoothSubarrays(int[] nums) {
      // write your code here
    }

}
Checking account…