Magicsheet logo

Percentage of Letter in String

Easy
95.5%
Updated 6/1/2025

Asked by 1 Company

Topics

Percentage of Letter in String

What is this problem about?

The Percentage of Letter in String problem asks you to find the percentage of occurrences of a given letter in a string (floor division). This easy coding problem tests basic string counting and integer division. The string interview pattern is demonstrated at its simplest.

Why is this asked in interviews?

American Express asks this as a quick programming warmup to verify basic string manipulation skills: counting character occurrences and computing percentages.

Algorithmic pattern used

Count and compute. Count occurrences of letter in s. Return count * 100 // len(s) (integer floor division for percentage).

Example explanation

s="foobar", letter='o'. Count of 'o'=2. Percentage = 2*100//6 = 200//6 = 33.

s="aaa", letter='a'. Count=3. 3*100//3 = 100.

s="abc", letter='d'. Count=0. 0*100//3 = 0.

Common mistakes candidates make

  • Using float division and then int() (may give wrong result for edge cases).
  • Using round() instead of floor() (problem asks for floor percentage).
  • Not handling empty string (problem guarantees non-empty).
  • Case sensitivity: only exact character matches count.

Interview preparation tip

Simple counting problems like this test accuracy and speed. The key detail is floor division (//), not rounding. Verify: count * 100 // len(s) gives integer percentage rounded down. For Python users, // naturally floors; for Java/C++ users, integer division floors automatically for positive values. Always double-check the division direction (floor vs ceiling vs round) in percentage problems.

Similar Questions