Chemical Formula Weight
Updated
Problem statement
You are given a chemical formula, an array elements of different element symbols, and an array weights of the same length. The weight of elements[i] is weights[i].
The formula is a list of terms written one after another. Each term is an element symbol followed by an optional count. A symbol is one uppercase letter, then zero or more lowercase letters. The count is a positive whole number written in digits. If the count is missing, it means 1. The same symbol may appear in more than one term, and every term counts. The formula has no parentheses. Every symbol in the formula appears in elements.
For each term, multiply its count by its element's weight. Return the sum of all these values. The sum can be large, so return it as a 64-bit integer.
Function
formulaWeight(formula: String, elements: String[], weights: int[]) → longExamples
Example 1
formula = "CH4"elements = ["C","H"]weights = [12,1]return = 16One C atom adds 12. The four H atoms add 4 together. The total weight is sixteen.
Example 2
formula = "H2O"elements = ["H","O"]weights = [1,2]return = 4The supplied weights give 2 × 1 + 1 × 2 = 4.
Example 3
formula = "Na2Cl"elements = ["Na","Cl"]weights = [23,35]return = 81The multi-letter symbols contribute 2 × 23 + 35 = 81.
Constraints
1 <= formula.length <= 50001 <= elements.length == weights.length <= 100- Each symbol is one uppercase letter followed by at most two lowercase letters.
1 <= weights[i] <= 10^6- Every explicit count is between
1and999999. - The answer fits in a signed 64-bit integer.
More Agoda Phone Screen questions
- Sort Ones, Twos, and Threes In PlacePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Can Place FlowersPHONE SCREEN · Seen Nov 2026
- Reverse Words in a StringPHONE SCREEN · Seen Nov 2026
- Disk Space Window AnalyzerPHONE SCREEN · Seen Sep 2026
- Shortest Subarray With the Full DegreePHONE SCREEN · Seen Sep 2026
- Enumerate Weekly Work SchedulesPHONE SCREEN · Seen Sep 2026
- Count Values Appearing Exactly OncePHONE SCREEN · Seen Sep 2026
- Longest All-Present Attendance StreakPHONE SCREEN · Seen Sep 2026
- Find the Pivot ElementPHONE SCREEN · Seen Sep 2026
- Maximum Planes Destroyed Before LandingPHONE SCREEN · Seen Jan 2026
Similar questions at other companies
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Video Cache Hit Rates from a CSV LogTubi · PHONE SCREEN
- Resolve Variable Equations with Dependency ErrorsApplied Intuition · PHONE SCREENApplied Intuition interview questions
- Chatbot Away-Status RefactorOpenAI · PHONE SCREENOpenAI interview questions
- Count Repeat Customers Across DatesAmazon · PHONE SCREEN
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- JSON Identity VerificationKikoff · PHONE SCREENKikoff interview questions
- Parse Query StringAirbnb · PHONE SCREENAirbnb interview questions