Validate a Package Barcode Using a Mod-10 Check Digit

Last updated: November 29, 2026

Quick Overview

A package barcode is a digit string whose last digit is a check digit: starting from the rightmost non-check digit and moving left, double every other digit (subtracting 9 if the result exceeds 9), sum everything including the check digit, and the code is valid if the total is divisible by 10. Implement the validator and a function that computes the correct check digit for a payload.

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

0

0

0 solved


A package barcode is a digit string whose last digit is a check digit: starting from the rightmost non-check digit and moving left, double every other digit (subtracting 9 if the result exceeds 9), sum everything including the check digit, and the code is valid if the total is divisible by 10. Implement the validator and a function that computes the correct check digit for a payload.

Amazon assessments often include short string and arithmetic problems grounded in logistics, and checksum validation is a natural one. The grader is looking for careful indexing from the right, clean input validation, and a second function that reuses the same logic.

What the Interviewer Expects
  • Iterate from the right with a clear rule for which digits double
  • Reject non-digit characters and too-short input
  • Reuse a helper for both validate and compute
  • Trace a known example by hand
  • Explain what classes of typos this checksum catches
Key Topics to Cover
Strings
Checksums
Modular Arithmetic
Input Validation
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
  • Which single-digit errors and transpositions does this scheme detect or miss?
  • How would you validate a million barcodes per second in a sorter?
  • How would you extend it to alphanumeric codes?
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. It must be all digits and at least 2 characters (payload plus check). Return a bool for validation. check_digit(payload) returns the single digit that makes payload + digit vali...

High-Level Design

Walk the digits from right to left with position index i starting at 0 for the check digit. Digits at odd positions (1, 3, 5 ...) are doubled, and if doubled value > 9 subtract 9. Sum all and test `% ...


Submit Your Answer
Markdown supported