BackmediumStackFlipkart

Evaluating Postfix Expressions Using a Stack Solution

Problem Statement

Given a string of postfix mathematical expressions containing single-digit numbers and basic arithmetic operators (+, -, *, /), implement a function that evaluates the expression using a stack data structure and returns the result as a string.

Example 1
Input
2 3 +
Output
5.0

Explanation: Step-by-step: with input '2 3 +', we first push 2 and 3 onto the stack, then pop them off and add them together, giving output 5.0

Example 2
Input
4 2 *
Output
8

Explanation: Step-by-step: with input '4 2 *', we first push 4 and 2 onto the stack, then pop them off and multiply them together, giving output 8

Constraints

  • The input string will contain at most 100 elements separated by spaces.
  • Each number in the input string is a single-digit integer between 0 and 9.
  • The input string is guaranteed to be a valid postfix expression.
  • Division by zero will not occur.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Evaluating Postfix Expressions Using a Stack — Problem Statement & Solution Guide

StackMediumLIFO
TimeO(n)
|
SpaceO(n)

Problem Description

Given a string of postfix mathematical expressions containing single-digit numbers and basic arithmetic operators (+, -, *, /), implement a function that evaluates the expression using a stack data structure and returns the result as a string.

Examples

Example 1

Input

2 3 +

Output

5.0

Explanation: Step-by-step: with input '2 3 +', we first push 2 and 3 onto the stack, then pop them off and add them together, giving output 5.0

Example 2

Input

4 2 *

Output

8

Explanation: Step-by-step: with input '4 2 *', we first push 4 and 2 onto the stack, then pop them off and multiply them together, giving output 8

Constraints

  • The input string will contain at most 100 elements separated by spaces.
  • Each number in the input string is a single-digit integer between 0 and 9.
  • The input string is guaranteed to be a valid postfix expression.
  • Division by zero will not occur.

Optimal Approach & Strategy

A more efficient approach is to use a stack to store the operands and evaluate the expression from left to right, resulting in a linear time complexity of O(n). This approach avoids repeated calculations and takes advantage of the LIFO pattern of the stack.

Brute Force Approach

A naive approach would involve using a recursive function to evaluate each subexpression, resulting in an O(n^2) time complexity. However, this approach is inefficient due to repeated calculations. Another brute-force approach would be to generate all possible orderings of the numbers and operators, then evaluate each one.

Verified Code Solutions

No solution currently available for JavaScript. Select another language tab above.

Asked in Top Tech Interviews

Flipkart

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.