PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Capital One

Implement string/matrix simulations and counting pairs

Last updated: Mar 29, 2026

Quick Overview

This multi-part question evaluates implementation skills in string processing (case-insensitive substring matching), grid simulation with iterative match-and-collapse rules and gravity, and combinatorial counting of ordered string-pair concatenations.

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

Implement string/matrix simulations and counting pairs

Company: Capital One

Role: Machine Learning Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given several independent implementation-focused tasks. For each task, write a function that returns the required value. ## Task 1 — Count matching 3-character windows (case-insensitive) Given a string `s`, consider every contiguous substring of length 3: `s[i..i+2]` for `0 ≤ i ≤ len(s)-3`. Count how many such windows satisfy: - `lower(s[i]) == lower(s[i+2])` (case-insensitive comparison) **Output:** an integer count. **Notes:** - If `len(s) < 3`, the answer is `0`. --- ## Task 2 — Grid “match-3” explosion with gravity You are given an `m x n` integer matrix `grid` where each integer represents a “color”. A cell `(r, c)` has up to 4 orthogonal neighbors: up, down, left, right. ### Explosion rule A cell `(r, c)` is considered a **trigger** if **at least 2** of its orthogonal neighbors have the **same value** as `grid[r][c]`. When a cell triggers, the cell itself and all orthogonal neighbors that match its value are marked to be removed. All removals for the step happen **simultaneously**. ### Gravity rule After removals, the removed positions become empty. Then, for each column independently: - All remaining values in the column “fall” downward (preserving their relative order). - Empty cells are filled at the top with `0`. ### Iteration Repeat **Explosion → Gravity** until no more cells trigger. **Output:** the final matrix after the process stabilizes. **Clarifications:** - `0` is treated as an empty cell after gravity; it does **not** participate in explosions. --- ## Task 3 — Count ordered pairs that form a target string Given a list of strings `words` of length `n` and a target string `target`, count the number of **ordered** index pairs `(i, j)` such that: - `0 ≤ i, j < n` - `i != j` - `words[i] + words[j] == target` **Output:** an integer count. **Important:** Ordered pairs are distinct, so `(i, j)` and `(j, i)` are counted separately when both satisfy the condition.

Quick Answer: This multi-part question evaluates implementation skills in string processing (case-insensitive substring matching), grid simulation with iterative match-and-collapse rules and gravity, and combinatorial counting of ordered string-pair concatenations.

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
Jan 12, 2026, 12:00 AM
Machine Learning Engineer
Take-home Project
Coding & Algorithms
4
0
Loading...

You are given several independent implementation-focused tasks. For each task, write a function that returns the required value.

Task 1 — Count matching 3-character windows (case-insensitive)

Given a string s, consider every contiguous substring of length 3: s[i..i+2] for 0 ≤ i ≤ len(s)-3.

Count how many such windows satisfy:

  • lower(s[i]) == lower(s[i+2]) (case-insensitive comparison)

Output: an integer count.

Notes:

  • If len(s) < 3 , the answer is 0 .

Task 2 — Grid “match-3” explosion with gravity

You are given an m x n integer matrix grid where each integer represents a “color”.

A cell (r, c) has up to 4 orthogonal neighbors: up, down, left, right.

Explosion rule

A cell (r, c) is considered a trigger if at least 2 of its orthogonal neighbors have the same value as grid[r][c].

When a cell triggers, the cell itself and all orthogonal neighbors that match its value are marked to be removed.

All removals for the step happen simultaneously.

Gravity rule

After removals, the removed positions become empty. Then, for each column independently:

  • All remaining values in the column “fall” downward (preserving their relative order).
  • Empty cells are filled at the top with 0 .

Iteration

Repeat Explosion → Gravity until no more cells trigger.

Output: the final matrix after the process stabilizes.

Clarifications:

  • 0 is treated as an empty cell after gravity; it does not participate in explosions.

Task 3 — Count ordered pairs that form a target string

Given a list of strings words of length n and a target string target, count the number of ordered index pairs (i, j) such that:

  • 0 ≤ i, j < n
  • i != j
  • words[i] + words[j] == target

Output: an integer count.

Important: Ordered pairs are distinct, so (i, j) and (j, i) are counted separately when both satisfy the condition.

Comments (0)

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 7,500+ 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.