Skip to content

Instantly share code, notes, and snippets.

@sahilsandhu
sahilsandhu / FAQs
Created January 11, 2022 10:11
Largest Area Histogram
1. How does using a stack decrease the time complexity?
ans: Because we are calculating the next smallest element by using a stack as we traverse through the array, using a stack
reduces the time complexity to linear time.
2. What is the space complexity of the program?
ans: We use two arrays to store the next smallest left and next smallest right respectively and use another array to store the areas.
So, the total space used is O(3n), which simplifies to O(n).
3. What if there will be no smaller element in left or right?
ans: Consider the value at index 1. To the left, there are no smaller elements. For index 1, we'll suppose the NSE to the left is -1.
@sahilsandhu
sahilsandhu / FAQ
Created January 11, 2022 09:59
Stock Span
1. If next greater left does not exist for an element, what should we take that for...
ans:- -1
2. How to implement a stack using queue?
ans: A stack can be implemented using two queues. Let stack to be implemented be ‘s’ and queues used to implement be ‘q1’ and ‘q2’.
Stack ‘s’ can be implemented in two ways:
Method 1 (By making push operation costly)
Method 2 (By making pop operation costly)
@sahilsandhu
sahilsandhu / FAQs
Created January 11, 2022 09:43
Sort 012
1. What will be the different sections that need to be mantained in this question?
ans: Here we need to mantain 4 sections of the array:
1. 1 to low (the range containing 0)
2. low to mid (the range containing 1)
3. mid to high (the range containing unknown elements),
4. high to N (the range containing unknown elements) (the range containing 2)
2. Someone explained to me that arrays were really just constant pointers.
ans: This is a bit of an oversimplification. An array name is ``constant'' in that it cannot be assigned to, but an array is not a
pointer.
@sahilsandhu
sahilsandhu / FAQ
Created January 11, 2022 09:19
Sort 01
1- What is time complexity of the question?
ans- O(n)
2- What is space complexity of the question?
ans- O(1)
3. difference between merge sort and quick sort?
ans: The fundamental difference between quicksort and merge sort is that quicksort sorts the elements by comparing each one to a
pivot, whereas merge sort divides the array into two subarrays repeatedly until only one element remains.
@sahilsandhu
sahilsandhu / FAQs
Last active January 4, 2022 09:37
Orderly Queue Easy
1. what is the time complexity of this problem?
ans: O(N)
2. Difference between StringBuilder and stringBuffer?
ans: StringBuffer is thread-safe since it is synchronised. It means that two threads can't call the StringBuffer functions at the same
time. StringBuilder is not thread safe because it is not synchronised. It indicates that two threads can use StringBuilder's methods at
the same time.
3. what is the meaning of lexicographic?
ans: The lexicographic or lexicographical order is a generalisation of dictionaries' alphabetical order to sequences of ordered symbols
@sahilsandhu
sahilsandhu / HINTs
Last active January 4, 2022 09:45
Maximum Product Subarray Easy
1. Try to thik of different conditions according to the requirement of the question.
2.. If A and B is positive and answer between them can be negative or positive. If answer is positive then
multiply the answer by A and B both. And if answer is negative then maximum product will be A or B any.
3. If A is positive and B is negative and answer between them can be positive or negative. If answer is positive then final
answer will be multiplied by B. And if answer is negative then final answer will be multiplied by A.
4.. If A is negative and B is positive and answer between them can be positive or negative. If answer is positive then final
answer will be multiplied by A. And if answer is negative then final answer will be multiplied by B.
5. If A is negative and B is negative and answer between them can be positive or negative. If answer is negative then final
answer will be multiplied by A or B both depending whichever will give maximum product. And if answer is positive then final
answer is answer itself.
@sahilsandhu
sahilsandhu / FAQ
Created January 3, 2022 09:10
Maximum Xor Of Two Numbers In An Array
1. what is the trie data structure?
Trie is an efficient information reTrieval data structure. Using Trie, search complexities can be brought to optimal limit (key length).
If we store keys in binary search tree, a well balanced BST will need time proportional to M * log N, where M is maximum string length
and N is number of keys in tree. Using Trie, we can search the key in O(M) time.
2. what is the maximum 32 bit number?
ans: 32 times 1 is the maximum 32 bit number.
3. what are the properties of XOR?
1 ^ 1 = 0
@sahilsandhu
sahilsandhu / FAQ
Created January 3, 2022 09:04
Find and Replace Pattern
1. What is HashSet?
ans: The HashSet class in Java is used to create a collection that stores data in a hash table. It derives from AbstractSet and implements
the Set interface.
2. What is HashMap?
ans: The HashMap class in Java implements the Map interface, which allows us to store key-value pairs with unique keys. If you try to
insert the duplicate key, it will overwrite the associated key's element. Updating, deleting, and other operations are simple to
accomplish with the key index. The java.util package contains the HashMap class.
3. What is the time complexity to access element form the HashSet?
@sahilsandhu
sahilsandhu / FAQs
Last active January 3, 2022 10:00
Bulb Switcher 4 IV Medium
1. what is the diference bewteen the 'a' and "A" ?
ans: 'a' is treated as a character by the compiler and "A" is treated as a string by the compiler.
2. what are all the appropirate conditions to solve this question?
ANS: We iterate the array and if the future element is same as current, ignore. Otherwise flip the elements and for that check.
If the future is 0 then flip the bulbs and turned them to 1. If future is 1 then flip the bulbs and turned them to 0.
3. what is the meaning of target.charAt(i) - '0'?
ans: It give the differnce of ascii value of target[i] and '0'.
@sahilsandhu
sahilsandhu / FAQs
Last active January 3, 2022 09:53
Battleship in Board Easy
1. The “X” at position (1,1), will only be considered into the battleship if its “I - 1” position is also non “X” and “J - 1“ position
is also non “X” then only we can say current “X” helps us in the battleship. Why?
Just because if the “X” will be present in any of the “I - 1” or “J - 1” positions, then the current position will already be
included as some other battleship part.
2. What are all the conditions the coder need to handle in this problem?
Ans: case 1: i==0 && j == 0
case 2 i == 0
case 3 j == 0
case 4 i!=0 && j!=0