Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Stacks

Problem Description

In computer science, a stack or LIFO (last in, first out) is an abstract data type that serves as a collection of elements, with two principal operations: push, which adds an element to the collection, and pop, which removes the last element that was added.(Wikipedia)

 

 

 

Syntax to create an Stack:

// Create Integer type stack
Stack < Integer > myStack = new Stack<>();

The above code create an stack named myStack which can store integers.

Some more functions which we can use with stack:

 

 

 

 

  • myStack.push(x) : Use push() to push an element into the stack.
  • myStack.pop() : To remove an element from the top of the stack, we use the pop() method.
  • myStack.peek() : The peek() method returns an object from the top of the stack.
  • myStack.empty() : To check whether a stack is empty or not, we use the empty() method.

 

 

Now, try to solve the below problem using stack :

 

Given a string A consisting only of ’(‘ and ’)’.

 

 

 

 

 

 

 

 

You need to find whether parantheses in A is balanced or not ,if it is balanced then print 1 else print 0.

Examples of some correctly balanced strings are: “()()”, “((()))”, “((()))”

Examples of some unbalanced strings are: “()(“, “(())”, “((“, “)(“ etc.

 

 

 

 

 

 

 

 



Problem Constraints

1 <= Number of Testcases <= 100

 

 

1 <= |A| <= 105

 

 



Input Format

First line of input consist of single integer T denoting the number of testcases.

 

 

Each testcase has a single line of input containing an string A.

 

 



Output Format

For each testcase output a single line i.e print 1 if parantheses in string are balanced else print 0.



Example Input

Input 1:

 

 

 

 

 

 

 2
 "(()())"
 "(()"

 

 



Example Output

Output 1:

 

 

 

 

 

 

 1
 0

 

 



Example Explanation

Explanation 1:

 

 

 

 

 

 

 Testcase 1: Given string (()()) is balanced so we print 1
 Testcase 2: Given string (() is not balanced so we print 0

 

 

 

 

 

 



Start solving Stacks on Interview Code Editor
Hints
  • Hint 1
  • Solution Approach
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test