Validate Card Numbers With the Luhn Checksum and Mask Them

Last updated: November 29, 2026

Quick Overview

Write a function that checks whether a card number string passes the Luhn checksum, allowing spaces and dashes as separators, and a helper that masks all but the last four digits for display.

Capital One
Coding & Algorithms
Software Engineer
Capital One
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Write a function that checks whether a card number string passes the Luhn checksum, allowing spaces and dashes as separators, and a helper that masks all but the last four digits for display.

Every card form in Capital One's web and mobile apps runs a Luhn check before sending a request, and masking to the last four digits is standard for PCI-sensitive display. This easy assessment item checks careful string handling, right-to-left indexing and input validation.

What the Interviewer Expects
  • Strip only the allowed separators
  • Reject non-digit characters and implausible lengths
  • Double every second digit counting from the right
  • Subtract 9 from doubled values above 9
  • Never log or return the full number unmasked
Key Topics to Cover
String parsing
Checksums
Input validation
PCI data handling
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you compute the check digit to append to a 15-digit prefix?
  • How would you identify the card network from the leading digits?
  • Why does passing Luhn not mean a card is real or active?
  • Where should validation live: client, server or both?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Accept digits with optional spaces or dashes; any other character makes the input invalid.
  • Card numbers are 12 to 19 digits long.
  • Return a boolean. Separately, mask a number as stars plus the la...
High-Level Design

Luhn works from the rightmost digit (the check digit). Every second digit moving left is doubled; if doubling produces a two-digit number, add its digits, which is the same as subtracting 9. The total...


Submit Your Answer
Markdown supported