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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 Feb 2026
Contest runs February 1 - 28. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.