Create a method that evaluates a mathematical expression from a string, respecting the order of operations (PEMDAS/BODMAS). The expression will only contain integers, addition (+), subtraction (-), multiplication (*), and division (/).
Examples:
Input: expression = "3 + 5 * 2" Output: 13 Explanation: Multiplication is performed before addition (5 * 2 = 10, then 3 + 10 = 13). Input: expression = "10 - 8 / 2" Output: 6 Explanation: Division is performed before subtraction (8 / 2 = 4, then 10 - 4 = 6). Input: expression = "2 * (3 + 4) - 5" Output: 9 Explanation: Operations in parentheses are performed first (3 + 4 = 7), then multiplication (2 * 7 = 14), and finally subtraction (14 - 5 = 9).
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.