Posts

Showing posts from July, 2021

Strings - Backspace String Compare using JAVA Stack

Question: Given two Strings S and T, return true if they equal when both are typed out any "#" will be treated as backspace. input - S -> ab#c, T -> az#c output - True Constraints need to ask because solution may change according to given constraints Question: What happens when 2 or multiple "#" appear besides each other? Answer: Delete same number of values before 1st "#". Ex- ab## -> "" Question: What happen to "#" when there is no character to remove? Answer: It deletes nothing. Ex- a###b -> "b" Question: Are two empty Strings equals each other? Answer: Ex- S -> a#b#c#, T -> c#. Both returns empty string and output will be true. Question: Does case sensitivity matters? Answer: Yes. Ex- A is different from a. S -> ab#c, T -> Az#c. Output - False Solution using JAVA language. BruteForce Approach - This is simple approach, We are using JAVA Stack here,...

Arrays - Trapping Rainwater - Optimal Solution using 2 Pointer Approach

Image
Question: Given an array of integers representing an elevation map (bar chart) where width of each bar is 1, return how much rain water can be trapped in empty space after raining? input [0,1,0,2,1,0,3,1,0,1,2] output 8 Constraints need to ask because solution may change according to given constraints Question: Sides of graph can be count as wall? Answer: No Question: Will there be negative integers? Answer: No Question: What will be output for empty array? Answer: 0 Question: What will be output when only 1 value in array? Answer: 0 Question: What will be output when input array like [3,4,3]? Answer: 0, no empty space to store water because walls cannot be used and water also fells from right side. Note: Max water can go to the mark is minimum height of bar between 2 bars and if there is a small bar pre...

Arrays - Trapping Rainwater - Brute Force Solution

Image
Question: Given an array of integers representing an elevation map (bar chart) where width of each bar is one, return how much rain water can be trapped in empty space. input [0,1,0,2,1,0,3,1,0,1,2] output 8 Constraints need to ask because solution may change according to given constraints Question: Sides of graph can be count as wall? Answer: No Question: Will there be negative integers? Answer: No Question: What will be output for empty array? Answer: 0 Question: What will be output when only 1 value in array? Answer: 0 Question: What will be output when input array like [3,4,3]? Answer: 0, no empty space to store water because walls cannot be used and water also fells from right side. Note: Max water can go to the mark is minimum height of bar between 2 bars and if there is a small bar present in between it will reduce the water quantity as shown in above image wit...

Arrays - Container with Most Water

Image
Question: Given an array of positive numbers where each number represent the height of a vertical line on a chart. Find two lines which together with the x-axis forms a container that would hold greatest amount of water. what will be the output which is max area of container formed by vertical lines. input [8, 2, 1, 5, 6, 9] output 40 Constraints need to ask because solution may change according to given constraints Question: Does the thickness of lines affect the are? Answer: No Question: Sides of graph can be count as wall? Answer: No Question: Does a higher line inside our container affect our area? Answer: No Question: What will be output for empty array? Answer: 0 Question: What will be output when only 1 value in array? Answer: 0 ...

Arrays - Two Sum

Question: Find a pair of index from given array where sum of numbers present at those indices is equal to given target number? input [4,2,5,8,9,6] target 7 output [1,2] Constraints need to ask because solution may change according to given constraints Question: If the given array only contains positive numbers or negative can be present? Answer: Only Positive numbers. Question: What would be output, If target number is not sum of any pair present in array? Answer: null Question: What will be output if only one integer present in given array, because question asked for pair of numbers? Answer: null Question: What is the output when given array is empty? Answer: null Question: Is there any duplicate number present in array? Answer: No Question: If multiple pair can have target sum, what will be output? Answer: First pair you get.