Convert Infix to Postfix
Updated
Problem statement
You are given an expression s in the usual infix form, where each operator sits between its two operands. Return the same expression in postfix form, where each operator comes right after its two operands.
Every operand is a single lower-case letter. The operators, from loosest to tightest, are: + and -; then * and /; then ^. Operators of the same level are grouped from left to right, except ^, which is grouped from right to left (so a^b^c means a^(b^c)). Parentheses make the part inside them one operand; they do not appear in the answer.
The answer has no spaces. For example, p*(q-r) becomes pqr-*.
Function
toPostfix(s: String) → StringExamples
Example 1
s = "x-y/z+w"return = "xyz/-w+"Division binds tighter, so y/z becomes yz/ first. Then - and + have the same level and are done left to right: x-(y/z) gives xyz/-, and adding w gives xyz/-w+.
Example 2
s = "(m+n)*k^j^i"return = "mn+kji^^*"The parentheses make m+n one operand: mn+. ^ groups from the right, so k^j^i means k^(j^i), which is kji^^. Multiplying the two parts gives mn+kji^^*.
Example 3
s = "t-(u-v)-s"return = "tuv--s-"The inner u-v is uv-. The two outer minus signs are done left to right: t minus that gives tuv--, and minus s gives tuv--s-.
Constraints
1 <= s.length <= 1999sconsists of lower-case English letters and the characters'+','-','*','/','^','('and')'.sis a valid expression: letters and operators alternate, starting and ending with a letter, where a valid expression inside a pair of parentheses also counts as a letter.- Parentheses are nested at most
100deep.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREENRoblox interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Rich Text Parser Part 3 (Depth Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Decode an Encoded StringAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Parse Indented YAML MappingsUber · PHONE SCREENUber interview questions
- Decode Repeated GroupsWaymo · PHONE SCREENWaymo interview questions
- Convert Email Text to Structured MarkupZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
- Parse Variable, Expression, and Application TreesSambaNova Systems · PHONE SCREEN · ONSITE INTERVIEWSambaNova Systems interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)