Skip to content

Latest commit

 

History

History
118 lines (79 loc) · 5.69 KB

dsalgo_problems_and_solutions.md

File metadata and controls

118 lines (79 loc) · 5.69 KB

DSAlgo problem and solutions - in a single list

Arrays

  • How do you find the missing number in a given integer array of 1 to 100?

  • How do you find the duplicate number on a given integer array ?

    • Naive solution would be compare each element in the list with other which is O(n*(n-1))
    • But better solution is to use Dict/Hashmap to keep track of duplicate items. O(n), considering each dict or Hashmap operation complexity would be O(1). In this solution there would be an extra space required.
  • How do you find the largest and smallest number in an unsorted integer array ?

    • With one iteration of the list/array we could figure out largest and smallest element. We would just need to keep track of largest and smallest element in each iteration of every item. Complexity would be O(n)
    • Or, we could sort the array and figure out the smallest and largest element with a[0] and a[len(a)-1]. Complexity would be in that case O(nlog(n))
    • http://www.java67.com/2014/02/how-to-find-largest-and-smallest-number-array-in-java.html
  • How do you find all pairs of an integer array whose sum is equal to a given number ?

  • How do you find duplicate numbers in an array if it contains multiple duplicates?

  • How do you remove duplicates from an array in place?

    • If we would have had an option to use extra space we could have used Dict/Hashmap like above.
    • But in this case we have to use the same array.
    • So we would require to sort the array in ascending order. Once it's done, then it we could check each neighbour element for duplicate.
    • https://www.geeksforgeeks.org/remove-duplicates-sorted-array/
  • How do you reverse an array in place ?

String

Trees