TikTok Interview Problem #4 — First Unique Character in a String|Interview Prep|Coding Challenge

42 Views
No Comments

A unique character is one that appears only once in a string.
Given a string consisting of lowercase English letters only, return the index of the first occurrence of a unique character in the string using 1-based indexing.
If the string does not contain any unique character, return -1.

Example:
s = "statistics"
The unique characters are [a, c], and a occurs first.
Using 1-based indexing, its index is 3.

Another Example:
Input: "hackthegame"
Unique characters: [c, k, t, g, m]
The character c occurs first at index 3.

The task is to find the first non-repeating character in a lowercase string and return its 1-based index.

Use a frequency count:

  1. Count occurrences of each character.
  2. Scan the string again; return the index of the first character with frequency 1.
  3. If none exists, return -1.

Runs in O(n) time and uses O(1) extra space.

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Tiktok, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Tiktok or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0