PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

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.

  • medium
  • Karat
  • Coding & Algorithms
  • Software Engineer

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.

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.

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

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

  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.
Last updated: Jun 26, 2026

Related Coding Questions

  • Implement obstacle-course run statistics - Karat (medium)
  • Join tables to map userId to name - Karat (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.