Skip to main content
All Questions

Calculate the weight of a molecule from its formula.

Unlock detailed company stats for this questionUpgrade

Calculate the weight of a molecule that contains only C, H and O, with weights 12, 1 and 8 respectively. The formula contains only C, H, O and digits; multi-atom counts are a single digit, for example CH4. Can be done with two pointers; a stack is not really needed.

Follow-up: formulas can now contain brackets, and the number outside a bracket multiplies the weight of the molecule inside, e.g. H(CH4)2 = 33. A stack is needed now.

Expect follow-ups like

  • Now the formula can contain brackets, and a number outside a bracket multiplies the enclosed molecule's weight, for example H(CH4)2 = 33.

Format · 60-minute HackerRank live coding with interviewer.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.