Find the letter whose second occurrence comes first in a lowercase string

Quick Overview

Given a lowercase string that contains at least one repeated letter, return the letter whose second occurrence comes earliest. It tests a single left-to-right scan with a set or bitmask and the difference between the earliest repeat and the earliest first occurrence.

Find the letter whose second occurrence comes first in a lowercase string

Company: Kroger

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given a string `s` consisting of lowercase English letters, return the first letter to appear twice. A letter `a` appears twice before another letter `b` if the **second occurrence** of `a` is before the second occurrence of `b`. `s` will contain at least one letter that appears twice. ### Function Signature ```python def repeated_character(s: str) -> str: ``` ### Rules - Return the letter as a one-character string. - Only the position of each letter's second occurrence matters. A letter that appears three or more times is ranked by its second occurrence. ### Constraints - `2 <= s.length <= 100` - `s` consists of lowercase English letters. - `s` has at least one repeated letter. ### Examples ```text Input: s = "abccbaacz" Output: "c" ``` Scanning left to right, `a`, `b` and `c` are new at indices 0 to 2, and `c` is seen again at index 3. The second `b` (index 4) and the second `a` (index 5) come later, so the answer is `c`. ```text Input: s = "abcdd" Output: "d" ``` Only `d` repeats, and its second occurrence is at index 4.

Overview: Given a lowercase string that contains at least one repeated letter, return the letter whose second occurrence comes earliest. It tests a single left-to-right scan with a set or bitmask and the difference between the earliest repeat and the earliest first occurrence.

Kroger logo
Kroger
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given a string s consisting of lowercase English letters, return the first letter to appear twice.

A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.

s will contain at least one letter that appears twice.

Function Signature

def repeated_character(s: str) -> str:

Rules

  • Return the letter as a one-character string.
  • Only the position of each letter's second occurrence matters. A letter that appears three or more times is ranked by its second occurrence.

Constraints

  • 2 <= s.length <= 100
  • s consists of lowercase English letters.
  • s has at least one repeated letter.

Examples

Input: s = "abccbaacz"
Output: "c"

Scanning left to right, a, b and c are new at indices 0 to 2, and c is seen again at index 3. The second b (index 4) and the second a (index 5) come later, so the answer is c.

Input: s = "abcdd"
Output: "d"

Only d repeats, and its second occurrence is at index 4.