AlgoMaster Logo
AlgoMasterRefactor a Password Checkereasy

Refactor a Password Checker

easy

Design, and more importantly refactor, a PasswordChecker for a signup flow.

The product has three password requirements today:

  • the password must contain at least 8 characters;
  • it must contain at least one ASCII digit from 0 to 9; and
  • it must not contain a space.

The legacy starter was built for an imagined future where administrators could assemble password rules at runtime. It includes a rule interface and speculative uppercase and symbol checks. Those features were never requested, and they now reject passwords the product considers valid.

Refactor the code to the smallest clear design that meets the current requirements:

  • PasswordChecker() creates a checker.
  • String check(String password) returns the first applicable result in this order:
    1. "TOO_SHORT"
    2. "MISSING_DIGIT"
    3. "HAS_SPACE"
    4. "OK"

Remove the speculative rules engine rather than extending it with another rule class. YAGNI is about keeping tomorrow's guesses out of today's design.

Example 1:

Input:

Output:

Explanation: Uppercase letters and symbols are allowed, but they are not required. The third password fails the rule that actually exists: no spaces.

Example 2:

Input:

Output:

Explanation: Checks run in a fixed order, so each password produces one useful result.

Constraints

  • 0 <= password.length <= 40
  • password contains only ASCII characters.
  • At most 100 calls will be made to check.

Starter Code

The starter compiles, but it solves a larger, hypothetical problem and enforces rules that do not belong here. Simplify it and add the missing space check.

How the design is graded

needs 7/10 to pass
  • Current requirements only

    Full marks when the checker enforces only minimum length, an ASCII digit and no spaces. Lose points heavily for uppercase, symbol, entropy or configurable-rule behavior that no requirement asks for.

  • Focused design

    Full marks for a small PasswordChecker with direct, readable checks. A private helper for digit detection is fine. Lose points for retaining the rule interface, registry, factory, priorities or runtime configuration.

  • Exact behavior

    Full marks when failures follow the required precedence and return the exact status strings. Lose points when speculative rules reject valid passwords, spaces are missed, or the class prints to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new PasswordChecker()null
check("Abcd123!")"OK"
check("abcd1234")"OK"
check("abcd 123")"HAS_SPACE"

Uppercase and symbols are not requirements. A space is rejected even when length and digit checks pass.

Run checks these cases. Submit also runs a larger hidden set.