Validate Debit Card Numbers With the Luhn Checksum

Last updated: November 29, 2026

Quick Overview

Given a card number string that may contain spaces or hyphens, return whether it is a well-formed card number that passes the Luhn checksum. Then write a helper that computes the check digit to append to a partial number.

SoFi
Coding & Algorithms
Software Engineer
SoFi
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a card number string that may contain spaces or hyphens, return whether it is a well-formed card number that passes the Luhn checksum. Then write a helper that computes the check digit to append to a partial number.

SoFi issues debit and credit cards and, through Galileo, processes card programs for many fintechs, so input validation on card numbers comes up as a warm-up in phone screens. The interviewer checks that you can implement a small spec precisely, handle messy input, and avoid off-by-one errors in which digits get doubled.

What the Interviewer Expects
  • Strips allowed separators and rejects any other non-digit character
  • Doubles every second digit counting from the rightmost digit, not the leftmost
  • Subtracts 9 (or sums digits) when a doubled value exceeds 9
  • Enforces a reasonable length range before computing the checksum
  • Explains why the check digit helper shifts which positions are doubled
Key Topics to Cover
Luhn algorithm
string validation
digit arithmetic
input normalization
PCI awareness
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 also infer the card network (Visa, Mastercard) from the prefix?
  • Why is Luhn useful for catching typos but useless as a security control?
  • How would you mask the number for logs so PCI scope stays small?
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

Input is a string such as "4539 1488 0343 6467". Spaces and hyphens are allowed separators; any other character makes the number invalid. Card numbers are 12 to 19 digits. Valid means: well formed a...

High-Level Design

Normalize first, then validate shape, then checksum. Walk the digits from the right with an index i: positions with odd i (the second, fourth, ... from the right) are doubled, and a doubled value ...


Submit Your Answer
Markdown supported