Validate password and list rule violations
Company: Karat
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string-processing and input-validation skills, including case-insensitive substring detection, character class checks, pattern matching for special symbols, and handling edge cases like consecutive character limits.
Constraints
- 0 <= len(p) <= 10^5
- specials contains exactly 3 distinct characters
- The password may contain any printable ASCII characters
- Rule numbers in the output must be in ascending order
Examples
Input: ("MyPassWord123!Xy", ['!', '@', '#'])
Expected Output: [2]
Explanation: Length 16 (>15 OK), has upper+lower, has '!', no long run — but contains 'password' case-insensitively, so only rule 2 is violated.
Input: ("Short1!Aa", ['!', '@', '#'])
Expected Output: [1]
Explanation: Length 9 (<=15) violates rule 1; all other rules pass (has upper, lower, '!', no 'password', no long run).
Input: ("GoodLongString99!", ['!', '@', '#'])
Expected Output: []
Explanation: Length 17, has upper+lower, has '!', no 'password' substring, no character repeats more than 4 times — fully compliant.
Input: ("alllowercase1234!", ['!', '@', '#'])
Expected Output: [3]
Explanation: Length 17 OK, has '!', no 'password', no long run, but has no uppercase letter, so rule 3 is violated.
Input: ("NoSpecialChars1234", ['!', '@', '#'])
Expected Output: [4]
Explanation: Length 18 OK, has upper+lower, no 'password', no long run, but contains none of '!','@','#', so rule 4 is violated.
Input: ("Aaaaaa1234567890!", ['!', '@', '#'])
Expected Output: [5]
Explanation: Length 17 OK, has upper+lower, has '!', no 'password', but 'a' repeats 5 times consecutively (>4), so rule 5 is violated.
Input: ("", ['!', '@', '#'])
Expected Output: [1, 3, 4]
Explanation: Empty string: violates rule 1 (length 0), rule 3 (no upper/lower), and rule 4 (no special). No 'password' substring and no consecutive run, so rules 2 and 5 pass.
Input: ("passwordPASSWORD", ['!', '@', '#'])
Expected Output: [2, 4]
Explanation: Length 16 (>15 OK) and has upper+lower, but contains 'password' (rule 2) and no special character (rule 4). No char repeats more than 4 times.
Input: ("aaaaaPassword!Bc1", ['!', '@', '#'])
Expected Output: [2, 5]
Explanation: Length 17 OK, has upper+lower, has '!'. But 'a' repeats 5 times (rule 5) and it contains 'password' case-insensitively (rule 2).
Hints
- Check each of the 5 rules independently and append its number to the result list in order — this naturally keeps the output sorted by rule number.
- For the 'password' substring check, lowercase the entire string first so the comparison is case-insensitive.
- For the consecutive-repeat rule, track the current run length: reset to 1 when the character changes, increment otherwise, and flag a violation the moment the run exceeds 4.