Infix to Postfix-Asked in Interviews

Problem Statement :

You are given a string expression. You are asked to print the postfix expression of the given string.

Output Format:

The output consists of one line which prints the given infix expression converted into a postfix expression.




  1. Scan the infix expression from left to right.
  2. If the scanned character is an operand, output it.
  3. Else,
    a) If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty or the stack contains a ‘(‘ ), push it.
    b) Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in the stack).
  4. If the scanned character is an ‘(‘, push it to the stack.
  5. If the scanned character is an ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis.
  6. Repeat steps 2–6 until infix expression is scanned.
  7. Print the output
  8. Pop and output from the stack until it is not empty.


Time Complexity: O(N)
Space Complexity: O(N*N)

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.




Upskilling students for tech placements!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Lessons learned on the 10 days out my #100daysofcode challenge.

Minimum Number of Swaps to Sort an Array of consecutive integers

How to find and register cheap domain for your project and not to overpay.

Ticket to Kotlin — building an online board game

Upon series keep increase candidate.

Developing and Deploying Frontend Code in Taobao: Eight-year Long Case Study Analysis

Funny with Taylor series

Do You Know How to Cast Accounts on the Cloud?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


Upskilling students for tech placements!

More from Medium

Longest Increasing Subsequence Coding Question

Linked List — A brief theoretical FAQ for Placement Interviews

Leetcode Q138. Copy List with Random Pointer

Leetcode 1514. Path with Maximum Probability