Solve OA tasks on string, grid path, subarrays
Company: Capital One
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Take-home Project
You are given three independent coding tasks.
## Task 1: Reverse the middle if ends are vowels
Given a string `s` (ASCII letters), if **both** the first and last characters are vowels (`a,e,i,o,u` case-insensitive), return a new string where:
- the first and last characters stay in place, and
- the substring strictly between them is reversed.
Otherwise, return `s` unchanged.
**Examples**
- `"apple"` → first=`a` (vowel), last=`e` (vowel) ⇒ reverse middle `"ppl"` → `"alppe"`
- `"code"` → first=`c` not vowel ⇒ `"code"`
**Edge cases**
- If `len(s) <= 2`, there is no “middle”; return `s`.
---
## Task 2: Maximize value along a constrained grid path
You are given an `m x n` grid of characters. Each cell contains either:
- a digit `'0'..'9'`, or
- an operator `'+'` or `'-'`.
You start at the **top-left** cell `(0,0)` and must reach the **bottom-right** cell `(m-1,n-1)` by moving only:
- **right**, or
- **down**.
As you traverse cells, you concatenate their characters to form an arithmetic expression, which is evaluated **left-to-right** (i.e., no operator precedence beyond sequential evaluation).
**Validity assumption:** Any path considered must form a valid expression of the form `digit (op digit)*` (alternating digit/operator), and it must start and end with a digit.
**Direction-change constraint:** Along the path, you are allowed to change direction **at most once** (i.e., the move sequence is either all rights then all downs, or all downs then all rights).
**Goal:** Return the **maximum** value achievable among all valid paths satisfying the movement constraint.
---
## Task 3: Count alternating odd/even subarrays
Given an integer array `nums`, count how many **contiguous subarrays** have strictly alternating parity (odd/even/odd/even... or even/odd/even/odd...).
A subarray of length 1 always counts.
**Example**
- `nums = [1, 2, 4]`
- Alternating subarrays: `[1]`, `[2]`, `[4]`, `[1,2]`, but `[2,4]` is not alternating (even, even), and `[1,2,4]` is not alternating.
- Answer = `4`
**Performance requirement:** Design an algorithm faster than brute force (e.g., `O(n)`).
Quick Answer: This multi-part question evaluates string manipulation, constrained grid-path optimization with sequential expression evaluation, and counting of parity-alternating subarrays, measuring competencies in algorithm design, dynamic programming or combinatorial path reasoning, and linear-time array techniques.