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 October 2026 (Oct 4–15)Sponsored by FLXBL

Contest runs October 4 - 15. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 12 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In