FastPrepNumber of Atoms
Problems/Number of Atoms

Number of Atoms

Amazon logoAmazon● HardONSITE INTERVIEW

Updated Jun 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a valid chemical formula string. Return the count of each atom as one canonical string.

An atom name starts with an uppercase letter and may be followed by lowercase letters. A number after an atom or a parenthesized group multiplies that atom or group. If no number follows, the multiplier is 1. Parentheses may be nested.

The output must list atom names in lexicographic order. For each atom, write the atom name followed by its count only when the count is greater than 1.

Function

countOfAtoms(formula: String) → String

Examples

Example 1

formula = "H2O"return = "H2O"

There are two hydrogen atoms and one oxygen atom. The count for oxygen is omitted because it is 1.

Example 2

formula = "Mg(OH)2"return = "H2MgO2"

The group (OH) is multiplied by 2, so the total counts are H:2, Mg:1, and O:2. Atom names are emitted in lexicographic order.

Constraints

  • The formula is valid and contains atom names, positive integer multipliers, and parentheses.
  • Parentheses may be nested.

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
  • Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
  • Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
  • Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREENRoblox interview questions
  • Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
  • Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
  • Common Words Across FilesMeta · PHONE SCREENMeta interview questions
  • Brazilian Receivables Part 3 — Partial ContractsStripe · PHONE SCREENStripe interview questions
  • Highlight and Rank Overlapping CitationsHarvey · ONSITE INTERVIEWHarvey interview questions
  • Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
  • Amazon SDE II (L5) Interview Questions: 5 Experiences
See Amazon hiring insights
Saved
class Solution {
    public String countOfAtoms(String formula) {
      // write your code here
    }
}
Checking account…