Author: devangini123
Problem Statement: You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation. Evaluate the expression. Return an integer that represents the value of the expression. Note that: The valid operators are ‘+’, ‘-‘, ‘*’, and ‘/’ . Each operand may be an integer or another expression. The division between two integers always truncates toward zero. There will not be any division by zero. The input represents a valid arithmetic expression in a reverse polish notation. The answer and all the intermediate calculations can be represented in a 32-bit integer. Examples: Example 1:…
Problem Statement: A valid parentheses string is either empty “”, “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings. A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B, with A and B nonempty valid parentheses strings. Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 + … + Pk, where Pi…
Problem Statement: A valid parentheses string is either empty “”, “(” + A + “)”, or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, “”, “()”, “(())()”, and “(()(()))” are all valid parentheses strings. A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B, with A and B nonempty valid parentheses strings. Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 + … + Pk, where Pi…
Problem Statement: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack()initializes the stack object. void push(int val)pushes the element val onto the stack. void pop()removes the element on the top of the stack. int top()gets the top element of the stack. int getMin()retrieves the minimum element in the stack. You must implement a solution with O(1) time complexity for each function. Examples: Example 1: Input: [“MinStack”,”push”,”push”,”push”,”getMin”,”pop”,”top”,”getMin”] [[],[-2],[0],[-3],[],[],[],[]] Output:[null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); //…
Problem Statement: Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order Every close bracket has a corresponding open bracket of the same type. Examples Example 1: Input:s = “()” Output:true Example 2: Input:s = “()[]{}” Output:true Example 3: Input:s = “(]” Output:false Example 4: Input:s = “([])” Output:true Constraints: 1top2 = -1; return q; } void myQueuePush(MyQueue* q, int x) {…
Problem Statement: Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x)Pushes element x to the back of the queue. int pop()Removes the element from the front of the queue and returns it. int peek()Returns the element at the front of the queue. boolean empty()Returns true if the queue is empty, false otherwise. Notes You must use only standard operations of a stack, which means only push to top, peek/pop from top, size and…
Problem Statement: Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x)Pushes element x to the top of the stack. int pop()Removes the element on the top of the stack and returns it.. int top()Returns the element on the top of the stack. boolean empty()Returns true if the stack is empty, false otherwise. Notes You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty…
Problem Statement: Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x)Pushes element x to the top of the stack. int pop()Removes the element on the top of the stack and returns it.. int top()Returns the element on the top of the stack. boolean empty()Returns true if the stack is empty, false otherwise. Notes You must use only standard operations of a queue, which means that only push to back, peek/pop from front, size and is empty…
Common Operations → Stack Push: Add an element to the top of the stack Pop: Remove the top-most element from the stack Peek/Top: View the top-most element without removing it Stack in JavaScript let stack = []; // Simply, Use Array as Stack. stack.push(1); // Add inside stack stack.push(2); stack.push(3); console.log(stack); // [1, 2, 3] stack.pop(); // A stack is LIFO (Last In, First Out), so the element that goes in last will come out first. In this case, 3 will be removed. stack.pop(); console.log(stack); // [1] stack.push(7); // [1, 7] // For Top element let top = stack[stack.length -…
What is a Stack? Stack follows the LIFO (Last-In-First-Out) principle. The last element added is the first one to come out. Stack Operations Push: Add an element to the top Pop: Remove the top element Peek/Top: View the top-most element Visualisation: Example Flow stack.push(1) → [1] stack.push(3) → [1, 3] stack.pop() → returns 3 → [1] stack.peek() → returns 1 Examples Stacks of Books Undo/Redo feature Recursion (Function Call Stack) Browsing History What is a Queue? Queue follows the FIFO (First-In-First-Out) principle. The first element added is the first to be removed. Queue Operations Enqueue: Add an element to the…
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.