Validate a payment transaction reference string

Quick Overview

Write a function that decides whether a payment transaction reference is valid: its length must be 8 to 20, it may hold only ASCII letters and digits, and no character may appear three times in a row. It tests careful string scanning, run-length tracking and boundary handling.

Validate a payment transaction reference string

Company: Adyen

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a payment reference string `reference`, validate whether it follows all of the rules for a valid transaction reference. A valid transaction reference must satisfy all of the following conditions: - It has a length between 8 and 20 characters (inclusive). - It contains only alphanumeric characters (letters `a`-`z`, `A`-`Z`, and digits `0`-`9`). - It does not contain a run of 3 or more consecutive identical characters. For example, `"aaa"` or `"111"` make a reference invalid, but `"aa"` or `"11"` are acceptable. Return `True` if the reference is valid, or `False` otherwise. ### Function Signature ```python def is_valid_reference(reference: str) -> bool: ``` ### Rules - Characters are compared exactly, so the run check is case-sensitive: `"aAa"` is not a run of identical characters, while `"AAA"` is. - Any character outside `a`-`z`, `A`-`Z` and `0`-`9` (a space, a hyphen, an underscore, punctuation) makes the reference invalid. - A reference that breaks several rules at once is simply invalid. The function returns one boolean. ### Constraints - `8 <= len(reference) <= 100` - `reference` consists of printable ASCII characters. ### Examples ```text Input: reference = "AB12CD34EF" Output: True ``` The string has length 10, which is between 8 and 20. Every character is a letter or a digit, and no character appears three times in a row. ```text Input: reference = "AAA12345BC" Output: False ``` The reference starts with `"AAA"`, a run of 3 consecutive identical characters, which breaks the repeated-run rule. ```text Input: reference = "AB12CD34EF56GH78IJ90K" Output: False ``` Every character is a letter or a digit and no two neighbors are equal, but the length is 21, one more than the limit of 20.

Overview: Write a function that decides whether a payment transaction reference is valid: its length must be 8 to 20, it may hold only ASCII letters and digits, and no character may appear three times in a row. It tests careful string scanning, run-length tracking and boundary handling.

Adyen logo
Adyen
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a payment reference string reference, validate whether it follows all of the rules for a valid transaction reference.

A valid transaction reference must satisfy all of the following conditions:

  • It has a length between 8 and 20 characters (inclusive).
  • It contains only alphanumeric characters (letters a - z , A - Z , and digits 0 - 9 ).
  • It does not contain a run of 3 or more consecutive identical characters. For example, "aaa" or "111" make a reference invalid, but "aa" or "11" are acceptable.

Return True if the reference is valid, or False otherwise.

Function Signature

def is_valid_reference(reference: str) -> bool:

Rules

  • Characters are compared exactly, so the run check is case-sensitive: "aAa" is not a run of identical characters, while "AAA" is.
  • Any character outside a - z , A - Z and 0 - 9 (a space, a hyphen, an underscore, punctuation) makes the reference invalid.
  • A reference that breaks several rules at once is simply invalid. The function returns one boolean.

Constraints

  • 8 <= len(reference) <= 100
  • reference consists of printable ASCII characters.

Examples

Input: reference = "AB12CD34EF"
Output: True

The string has length 10, which is between 8 and 20. Every character is a letter or a digit, and no character appears three times in a row.

Input: reference = "AAA12345BC"
Output: False

The reference starts with "AAA", a run of 3 consecutive identical characters, which breaks the repeated-run rule.

Input: reference = "AB12CD34EF56GH78IJ90K"
Output: False

Every character is a letter or a digit and no two neighbors are equal, but the length is 21, one more than the limit of 20.