Posts

Showing posts from October, 2024

interview tips

1.) tell soln from BRUTE to BETTER to OPTIMAL 2.) Give good names to fn &  var. 3.) Extra Space Used  :   given wale ko exclude karke batana hai. Space Used  : given wale ko include karke batana hai. 4.) never deep down in brute solution in interview, just tell upar upar se (IMP) 5.) tell method/steps upar upar se and u need not code in interview also (IMP) 6.)  Don't make global variables while solving an interview ques. ------------------------------------------------------------------------------------------------------------- 7. don't change original given thing, instead make a copy of it & work on copy ⭐8. we can simplify our soln by explicitly handling edge cases and making generalized algo for the result 9. in general, interviewer do not ask for time complexity for reccurssion problem, but if asked then tell exponential 10. Do not use pow() function in interview, instead build your own multiplier function, e.g., in nth root problem, we do ...

12. Arrays (easy)

Image
int arr[n]==> garbage values if inside main                ==> 0s if globally initialised -------------------------------------------------------------------------------------------------------------                            1.  Remove duplicates from Sorted array : Push all unique numbers at start and whatever at the end of uniques.... Brute   :(nlogn) array se set mein daal aur fir set se array mein... Optimal -O(n):⭐ 2 pointer approach whichever j ≠ i  ,( swap that j with i+1 ), or, ( put a[i+1]=a[j] )  --------------------------------------------------------------------------------------------------------------- 2. Left Rotate an array  by 1 place: [ 1 ,2,3,4,5]----->[2,3,4,5, 1 ] ----------------------------------------------------------------------------------------------------------------------------       3.  Left r...

11. Quick sort⭐

Image
Smart Divide & Conquer ( time- NlogN,    space- 1 ) pick a pivot & place it in its correct position in sorted array -------------------------------------------------------------------------------------------------------------------------- 1. make low and high 2. choose pivot 3. take 2 pointers i=low & j=high 4. if, a[i] > pivot  &    a[j]<= pivot,  swap the two else i++ and j-- 5. for one round do this till,  i>j 6. last mein pivot se swap karo 7. ab partition bangya, so  put pivot in right place--> sort left and right half ------------------------------------------------------------------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------

10. merge sort

Image
DIVIDE & CONQUER time complexity- O (NlogN) space complexity- O(N) ------------------------------------------------------------------------------------------------------------ For Splitting:- 1. We pass low and high to the function 2. find middle 3. recurse for ( low to mid ) & ( mid+1 to high ) 4. call merge function for these low, mid, high 5. done -------------------------------------------------------------------------------------------------------------------------- For merging:- we give the function: low, mid, high and break the array into  intervals : (left to mid), (mid+1 to high)  and then use 2 pointer ---------------------- temp goes from i=low to i=high, so at last, we store numbers in array using i-low to make it 0 based index --------------------------------------------------------------------------------------------------------------------------

9. bubble, selection, insertion SORT

Image
Selection Sort  O(N^2) 1. Select minimum from curr till end 2. Swap minimum wala with curr 3. Repeat for n-1 elements ---------------------------------------------------------------------------------------------------------------                                                                                                                            Bubble Sort  O(N^2) 🥤 below is code for optimised bubble sort since after each iteration in bubble sort, the largest element reaches the end ----------------------------------------------------------------------------------------------------------------------------- ...

8. Hashing

Image
Hash Array: pre-storing in a hash array/ freq array to reduce time complexity ------------------------------------------------------------------------------------------------------------- Character Hashing-  1.  without hashing-   2.   with hashing-       or works only if 's' contains a to z                                     works for all characters: A to Z, a to z, others... -------------------------------------------------------------------------------------------------------------                                                                         Mapping- ------------------------------------------------...

7. Basic Recursion

Image
1. base case 2. condition to check in each iteration to quit early 3. return statement ------------------------------------------------------------------------------------------------------------        Reversal of array- -----------------------------------------------------------------------------------------------------------------------------         Palindrome string check - -----------------------------------------------------------------------------------------------------------------------------                                                                                          Fibonacci -  time complexity: O(2^n):- exponential --------------------------------------...