Filter out passwords that appear k or more times, keeping first-appearance order

Quick Overview

Given a list of passwords and a threshold k, return the passwords that occur fewer than k times, each listed once in order of first appearance. It tests frequency counting with a hash map, a strict threshold comparison, and order-preserving deduplication.

Filter out passwords that appear k or more times, keeping first-appearance order

Company: IBM

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of strings `passwords` and an integer `k`, filter out any password that appears `k` or more times in the array. Return the list of **accepted passwords** (those appearing fewer than `k` times). The result should maintain the order of first appearance of each password, and each accepted password should appear only once in the output. ### Function Signature ```python def filter_passwords(passwords: list[str], k: int) -> list[str]: ``` ### Rules - Two entries are the same password only when the strings are identical, character for character. - A password that appears exactly `k` times is rejected. - Accepted passwords are ordered by the index of their first occurrence in `passwords`. If nothing is accepted, return an empty list. ### Constraints - `1 <= passwords.length <= 10^5` - `1 <= passwords[i].length <= 100` - `1 <= k <= 10^5` - `passwords[i]` consists of lowercase English letters, digits, and special characters. ### Examples ```text Input: passwords = ["abc","def","abc","ghi","abc","def"], k = 3 Output: ["def","ghi"] ``` `"abc"` appears 3 times, which reaches `k = 3`, so it is rejected. `"def"` appears 2 times and `"ghi"` once, so both are accepted, in the order they first appear (index 1, then index 3), and `"def"` is listed once. ```text Input: passwords = ["pass1","pass2","pass1","pass2","pass3"], k = 2 Output: ["pass3"] ``` `"pass1"` and `"pass2"` each appear 2 times, which reaches `k = 2`, so both are rejected. `"pass3"` appears once and is the only accepted password. ```text Input: passwords = ["q1","q2","q1"], k = 1 Output: [] ``` With `k = 1`, every password appears at least once, so every password is rejected and the result is empty.

Overview: Given a list of passwords and a threshold k, return the passwords that occur fewer than k times, each listed once in order of first appearance. It tests frequency counting with a hash map, a strict threshold comparison, and order-preserving deduplication.

IBM logo
IBM
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of strings passwords and an integer k, filter out any password that appears k or more times in the array.

Return the list of accepted passwords (those appearing fewer than k times). The result should maintain the order of first appearance of each password, and each accepted password should appear only once in the output.

Function Signature

def filter_passwords(passwords: list[str], k: int) -> list[str]:

Rules

  • Two entries are the same password only when the strings are identical, character for character.
  • A password that appears exactly k times is rejected.
  • Accepted passwords are ordered by the index of their first occurrence in passwords . If nothing is accepted, return an empty list.

Constraints

  • 1 <= passwords.length <= 10^5
  • 1 <= passwords[i].length <= 100
  • 1 <= k <= 10^5
  • passwords[i] consists of lowercase English letters, digits, and special characters.

Examples

Input: passwords = ["abc","def","abc","ghi","abc","def"], k = 3
Output: ["def","ghi"]

"abc" appears 3 times, which reaches k = 3, so it is rejected. "def" appears 2 times and "ghi" once, so both are accepted, in the order they first appear (index 1, then index 3), and "def" is listed once.

Input: passwords = ["pass1","pass2","pass1","pass2","pass3"], k = 2
Output: ["pass3"]

"pass1" and "pass2" each appear 2 times, which reaches k = 2, so both are rejected. "pass3" appears once and is the only accepted password.

Input: passwords = ["q1","q2","q1"], k = 1
Output: []

With k = 1, every password appears at least once, so every password is rejected and the result is empty.