PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Capital One

Maximize grid-path expression and count sawtooth subarrays

Last updated: Mar 29, 2026

Quick Overview

This multi-part question evaluates dynamic programming and state management for constrained grid traversal with left-to-right expression evaluation, alongside parity-based array analysis and combinatorial counting of contiguous subarrays.

  • medium
  • Capital One
  • Coding & Algorithms
  • Machine Learning Engineer

Maximize grid-path expression and count sawtooth subarrays

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: HR Screen

## Problem 1: Maximize a valid expression along a grid path You are given an `m x n` grid `grid`. Each cell contains either: - an operator: `'+'` or `'-'`, or - a single digit character `'0'` … `'9'`. You start at the top-left cell `(0,0)` and must reach the bottom-right cell `(m-1,n-1)` by moving only: - **right** `(r, c) -> (r, c+1)`, or - **down** `(r, c) -> (r+1, c)`. As you traverse a path, you concatenate the visited cells (in order) to form an expression. An expression is **valid** if it alternates strictly between numbers and operators: - It must start with a digit. - It must then alternate: `digit, operator, digit, operator, digit, ...` - It must end with a digit. - Therefore, it may **not** contain: - two consecutive operators (e.g., `0 ++ 1`, `3 +- 4`), or - two consecutive digits (e.g., `1 2 + 3`, `5 - 6 3`). **Evaluation rule:** Evaluate the expression strictly left-to-right (since only `+` and `-` are present). ### Task Return the **maximum possible evaluated value** among all **valid** paths from `(0,0)` to `(m-1,n-1)`. ### Assumptions / constraints (for clarity) - `1 <= m, n <= 50` - `grid[r][c]` is one of `{'+','-','0'..'9'}` - If no valid path exists, return an agreed sentinel (e.g., `null` / `None` / `-inf`) as specified by the interviewer. --- ## Problem 2: Count sawtooth (alternating parity) subarrays A **sawtooth sequence** is a sequence of integers where adjacent elements have **different parity** (even/odd), i.e., it alternates between even and odd. Given an integer array `arr`, count the number of **contiguous subarrays** that are sawtooth sequences. - Subarrays of length **1** count as sawtooth by definition. ### Examples - `arr = [1, 3, 5, 7, 9]` → output `5` (only the 5 single-element subarrays). - `arr = [1, 2, 1, 2, 1]` → output `15` (all subarrays alternate parity). - `arr = [1, 2, 3, 7, 6, 5]` → output `12`. ### Constraints (for clarity) - `1 <= len(arr) <= 2 * 10^5` - `|arr[i]| <= 10^9` ### Task Return the total number of contiguous sawtooth subarrays.

Quick Answer: This multi-part question evaluates dynamic programming and state management for constrained grid traversal with left-to-right expression evaluation, alongside parity-based array analysis and combinatorial counting of contiguous subarrays.

Related Interview Questions

  • Solve Four Coding Assessment Tasks - Capital One (medium)
  • Write SQL using joins and window functions - Capital One (medium)
  • Review Preprocessing Code and Tests - Capital One (easy)
  • Remove nodes with a given value - Capital One (medium)
  • Solve multiple algorithmic interview questions - Capital One (hard)
Capital One logo
Capital One
Oct 13, 2025, 12:00 AM
Machine Learning Engineer
HR Screen
Coding & Algorithms
10
0

Problem 1: Maximize a valid expression along a grid path

You are given an m x n grid grid. Each cell contains either:

  • an operator: '+' or '-' , or
  • a single digit character '0' … '9' .

You start at the top-left cell (0,0) and must reach the bottom-right cell (m-1,n-1) by moving only:

  • right (r, c) -> (r, c+1) , or
  • down (r, c) -> (r+1, c) .

As you traverse a path, you concatenate the visited cells (in order) to form an expression.

An expression is valid if it alternates strictly between numbers and operators:

  • It must start with a digit.
  • It must then alternate: digit, operator, digit, operator, digit, ...
  • It must end with a digit.
  • Therefore, it may not contain:
    • two consecutive operators (e.g., 0 ++ 1 , 3 +- 4 ), or
    • two consecutive digits (e.g., 1 2 + 3 , 5 - 6 3 ).

Evaluation rule: Evaluate the expression strictly left-to-right (since only + and - are present).

Task

Return the maximum possible evaluated value among all valid paths from (0,0) to (m-1,n-1).

Assumptions / constraints (for clarity)

  • 1 <= m, n <= 50
  • grid[r][c] is one of {'+','-','0'..'9'}
  • If no valid path exists, return an agreed sentinel (e.g., null / None / -inf ) as specified by the interviewer.

Problem 2: Count sawtooth (alternating parity) subarrays

A sawtooth sequence is a sequence of integers where adjacent elements have different parity (even/odd), i.e., it alternates between even and odd.

Given an integer array arr, count the number of contiguous subarrays that are sawtooth sequences.

  • Subarrays of length 1 count as sawtooth by definition.

Examples

  • arr = [1, 3, 5, 7, 9] → output 5 (only the 5 single-element subarrays).
  • arr = [1, 2, 1, 2, 1] → output 15 (all subarrays alternate parity).
  • arr = [1, 2, 3, 7, 6, 5] → output 12 .

Constraints (for clarity)

  • 1 <= len(arr) <= 2 * 10^5
  • |arr[i]| <= 10^9

Task

Return the total number of contiguous sawtooth subarrays.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Capital One•More Machine Learning Engineer•Capital One Machine Learning Engineer•Capital One 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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.