Reported July 2026
Amazonstack

Number of Atoms

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The Amazon OA reported in July 2026 hands you a chemical formula like Mg(OH)2 and wants a sorted count string back. It's Number of Atoms, and the catch is nested parentheses with multipliers after them. If you've seen a stack-based parser before, this is routine. If you haven't, it can eat your clock fast. The parsing is simple once you see the shape, and the output rule (omit counts of 1, sort names) is where sloppy code loses points. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution as a safety net.

The problem

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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is a stack of hash maps. Start with one map. When you hit an open paren, push a fresh map. When you hit a close paren, read the number after it (default 1), pop the top map, multiply every count by that number, and merge it into the map below. Atoms are an uppercase letter plus any lowercase letters, followed by optional digits. Counts can be multi-digit, so don't read a single character. The common pitfall is forgetting the default multiplier of 1, or reading only one digit. The second is the output: sort keys lexicographically, then append the count only when it's above 1. Sorting at the end is cheap and clean. If the parser logic slips under pressure, StealthCoder is the hedge for the live OA, since it reads the problem on screen and gives you the stack solution without the proctor seeing it.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Number of Atoms cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of atoms. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Atoms FAQ

How hard is Number of Atoms really?+

It's a hard-tagged problem, but the difficulty is bookkeeping, not an algorithm. There's no clever math. You parse left to right with a stack of maps. Most failures come from multi-digit numbers, nested groups, and the omit-if-1 output rule, not from the core idea.

What's the trick to handle nested parentheses?+

Push a new count map on every open paren. On a close paren, read the trailing number, pop the map, multiply each count, and add it into the map below. Nesting works automatically because each level merges into its parent when it closes.

How do I parse atom names and numbers correctly?+

An atom starts with an uppercase letter, then consume all following lowercase letters. Next consume all consecutive digits as one integer. If no digits exist, use 1. Same digit-reading logic applies after a closing paren, so write one helper and reuse it.

How should I format the output string?+

Sort the atom names lexicographically, then build the string by appending the name and the count only when the count is greater than 1. Example: H2MgO2 for Mg(OH)2. Plain string sort works since names start with uppercase letters.

How do I prepare for this in 48 hours?+

Write the stack-of-maps solution once from scratch, then test Mg(OH)2, nested cases like K4(ON(SO3)2)2, and multi-digit multipliers. Check the omit-1 output rule. That covers the failure points. Amazon reported this in July 2026, so expect the same shape.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it