PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skills in string manipulation, frequency analysis, and optimization of weighted sums, focusing on assigning distinct values to characters to maximize an aggregate metric.

  • Medium
  • Bank of America
  • Coding & Algorithms
  • Data Scientist

Compute maximum beauty of a string

Company: Bank of America

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

Given a string s, define the beauty of each letter a–z as a distinct integer between 1 and 26, case-insensitive. The beauty of a string is the sum over letters of (letter beauty × its frequency). Ignore punctuation and spaces. For each input line containing a string, compute and print the maximum possible beauty by choosing an assignment of beauties to letters that maximizes the sum. Examples: Input "ABbCCC" → 152; Input "Ignore punctuation, please :)" → 491.

Quick Answer: This question evaluates skills in string manipulation, frequency analysis, and optimization of weighted sums, focusing on assigning distinct values to characters to maximize an aggregate metric.

Little Johnny defines the beauty of a string as the sum of the beauty of the letters in it. The beauty of each letter is a distinct integer between 1 and 26 inclusive, and no two letters share the same beauty value. Letter case is ignored (uppercase 'F' is exactly as beautiful as lowercase 'f'), and any character that is not a letter (punctuation, spaces, digits) is ignored. Given a string `s`, choose an assignment of beauty values to the distinct letters that maximizes the total beauty, and return that maximum possible beauty. The total beauty equals the sum over every distinct letter of (that letter's assigned beauty value x the number of times it appears in `s`). To maximize the total, assign the largest beauty value (26) to the most frequent letter, the next largest (25) to the second most frequent, and so on. Example 1: `s = "ABbCCC"` -> the letter frequencies are C:3, B:2, A:1. Assign 26 to C, 25 to B, 24 to A: 26*3 + 25*2 + 24*1 = 78 + 50 + 24 = 152. Example 2: `s = "Ignore punctuation, please :)"` -> 491.

Constraints

  • The string may contain uppercase letters, lowercase letters, spaces, punctuation, and digits.
  • Letter comparison is case-insensitive: 'A' and 'a' count as the same letter.
  • Only alphabetic characters (a-z, A-Z) contribute to beauty; everything else is ignored.
  • There are at most 26 distinct letters, so beauty values 26, 25, ... are assigned greedily by descending frequency.
  • An empty string, or a string with no letters, has a beauty of 0.

Examples

Input: "ABbCCC"

Expected Output: 152

Explanation: Frequencies C:3, B:2, A:1. Assign 26*3 + 25*2 + 24*1 = 78 + 50 + 24 = 152.

Input: "Ignore punctuation, please :)"

Expected Output: 491

Explanation: Only letters count; the comma, space, and ':)' are ignored. Greedily assigning 26, 25, 24, ... to letters by descending frequency yields 491.

Input: ""

Expected Output: 0

Explanation: An empty string has no letters, so its beauty is 0.

Input: "!!! ,,, 12345"

Expected Output: 0

Explanation: There are no alphabetic characters; punctuation, spaces, and digits are all ignored, so the beauty is 0.

Input: "a"

Expected Output: 26

Explanation: A single distinct letter gets the maximum beauty value 26, appearing once: 26*1 = 26.

Input: "aaaA"

Expected Output: 104

Explanation: Case-insensitively, 'a' appears 4 times. The single distinct letter gets value 26: 26*4 = 104.

Hints

  1. The beauty assignment is a permutation of 1..26 over the distinct letters. To maximize a sum of (value x frequency), pair the largest values with the largest frequencies (rearrangement inequality).
  2. You never need to know which specific letter gets which value -- only the multiset of letter frequencies matters.
  3. Count case-insensitive letter frequencies, sort them in descending order, then multiply the i-th largest frequency by (26 - i) and sum.
  4. Be careful to ignore spaces, punctuation, and digits, and to treat uppercase and lowercase as the same letter.
Last updated: Jun 26, 2026

Related Coding Questions

  • Convert number words to integer - Bank of America (Medium)
  • Explain coding solution and alternatives - Bank of America (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
  • Compare Platforms
  • Discord Community

Support

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

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.