← Capital One Interview Insights

Capital One·Machine Learning Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Capital One ML Engineer interview with a coding round that had at least one array manipulation problem. Nothing too exotic but the medium-difficulty question had a subtle algorithmic angle that took me a minute to see clearly.

Questions Asked (1)

Q1

You have an array of house heights. To 'beautify' a block, the heights must be either strictly increasing by 1 or strictly decreasing by 1 from left to right. Each operation lets you increment a single house's height by 1. What is the minimum number of operations needed?

Algorithms & Data Structures
Author's notes

The key insight I kept second-guessing was that you can only increment, never decrement.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that the final array must be either an arithmetic progression with common difference +1 or -1. For each possible starting height, compute the cost to transform the given array into each of these two patterns, then take the minimum. Since only increments are allowed, the starting height must be at least the maximum of (height[i] - i) for increasing pattern and (height[i] + i) for decreasing pattern, so we can compute the minimal cost directly without iterating over all possible starts.

Pro tip: Clarify that only increments are allowed, so the target heights must be at least the original heights. This constraint simplifies the problem: for each pattern, the optimal starting height is determined by the maximum required offset, and the cost is the sum of differences.

1. Understand the target patterns

The beautified array must be strictly increasing by 1 (i.e., h[i] = start + i) or strictly decreasing by 1 (i.e., h[i] = start - i). Identify these two possible patterns.

2. Determine constraints for start

Since only increments are allowed, for the increasing pattern, start must satisfy start + i >= h[i] for all i, so start >= max(h[i] - i). For the decreasing pattern, start - i >= h[i], so start >= max(h[i] + i).

3. Compute minimal cost for each pattern

For each pattern, set start to the minimal feasible value (the maximum of the required lower bounds). Then compute the total operations as the sum over i of (target[i] - h[i]).

4. Return the minimum of the two costs

Compare the costs for the increasing and decreasing patterns and return the smaller one as the answer.

Key Points to Mention

  • The final array must be an arithmetic progression with common difference +1 or -1.
  • Only increment operations are allowed, so the target heights must be >= original heights.
  • For each pattern, the optimal starting height is the maximum of (h[i] - i) for increasing or (h[i] + i) for decreasing.
  • The cost for a pattern is the sum of differences between target and original heights.
  • Time complexity is O(n) and space complexity O(1).
  • Edge cases: empty array, single element, already beautified array.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.