As reported: Morgan Stanley Associate Java backend HackerRank OA (Apr 2025, 70 minutes, 4 sections: Java multiple choice, SQL, Java debugging, coding). Coding section: given a list of passwords, keep the valid ones — longer than 5 characters, and not made only of letters or only of digits. The tricky case: passwords made only of special characters (like
@#$%) are valid.
Given a list of strings passwords, return the valid ones in their original order. A password is valid when:
- its length is greater than 5, and
- it does not consist only of letters, and
- it does not consist only of digits.
A password made only of special characters, or of any mix (letters + digits, letters + specials, …), passes rules 2 and 3.
Example 1
passwords = ["abc123","abcdef","123456","@#$%^&","ab1","Pass@1"]
-> ["abc123","@#$%^&","Pass@1"]
"abcdef" is letters only, "123456" digits only, "ab1" too short.
Example 2
passwords = ["password","pass12","12345","!!!!!!"]
-> ["pass12","!!!!!!"]
Clarifications
- Letters are
A–Zanda–z; digits are0–9; every other character is a special character.
Constraints: 0 ≤ passwords.length ≤ 5000; each password has 1 to 20 printable ASCII characters, without spaces, quotes or backslashes.
Code runner format: one JSON value per line: passwords. Implement validPasswords(passwords); the harness prints the returned list.