Quick Overview

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.

Validate password and list rule violations

Company: Karat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem You are given a password string `p`. Validate it against a set of rules and return **all** rules that the password violates. ## Rules 1. **Length** must be **greater than 15** characters. 2. Must **not** contain the substring **"password"** (case-insensitive). For example, `"MyPassWord123"` violates this rule. 3. Must contain **at least one uppercase** letter (`A-Z`) and **at least one lowercase** letter (`a-z`). 4. Must contain **at least one** character from a given set of **three special symbols** (the set will be provided by the interviewer; e.g., `{!, @, #}`). 5. **No character** may repeat **more than 4 times consecutively** (e.g., `"aaaaa"` violates; `"aaaa"` is OK). ## Input - A string `p`. - A set/list of exactly 3 allowed special characters. ## Output - A list of violated rules (as strings or codes) in a deterministic order (e.g., rule number order). ## Notes - If the password is fully compliant, return an empty list. - Treat the "password" check as case-insensitive.

Overview: 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.

Read the full Karat Software Engineer interview experience this question came from

You are given a password string `p` and a list of exactly 3 allowed special characters. Validate the password against the following rules and return a list of the rule numbers that the password violates, in ascending rule-number order. Rules: 1. Length must be strictly greater than 15 characters. 2. The password must NOT contain the substring "password" (case-insensitive). For example, "MyPassWord123" violates this rule. 3. The password must contain at least one uppercase letter (A-Z) AND at least one lowercase letter (a-z). 4. The password must contain at least one character from the given set of 3 special characters. 5. No character may repeat more than 4 times consecutively (e.g., "aaaaa" violates; "aaaa" is OK). Return an empty list if the password is fully compliant. The returned list must contain the violated rule numbers in ascending order.

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).

Hints

  1. 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.
  2. For the 'password' substring check, lowercase the entire string first so the comparison is case-insensitive.
  3. 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.

Loading coding console...