Solving Coding Interview Questions in Python on LeetCode (easy & medium problems)

    11
    47



    In this video we walk through a series of eight coding interview questions on leetcode. These are algorithms problems that cover topics including data structures, time & space complexity, sorting, binary search, object oriented programming, breadth first search, tree traversals, and more. The difficulty of problems range from easy to medium and get gradually harder as you go through the video.

    Practice your Python Pandas data science skills with problems on StrataScratch!
    https://stratascratch.com/?via=keith

    Join the Python Army to get access to perks!
    YouTube – https://www.youtube.com/channel/UCq6XkhO5SZ66N04IcPbqNcw/join
    Patreon – https://www.patreon.com/keithgalli

    If you enjoy this video, make sure to SUBSCRIBE to not miss future videos.

    Checkout out my new channel where I will frequently be posting problems like these: https://www.youtube.com/channel/UCge9kgmp38FIjGK4xdJsThw/about

    Feel free to recommend the next problems that I should solve on YouTube!

    Links to problems:
    LeetCode 1678: https://leetcode.com/problems/goal-parser-interpretation/
    LeetCode 1436: https://leetcode.com/problems/destination-city/
    LeetCode 1365: https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
    LeetCode 704: https://leetcode.com/problems/binary-search/
    LeetCode 409: https://leetcode.com/problems/longest-palindrome/
    LeetCode 1561: https://leetcode.com/problems/maximum-number-of-coins-you-can-get/
    LeetCode 1472: https://leetcode.com/problems/design-browser-history/
    LeetCode 1110: https://leetcode.com/problems/delete-nodes-and-return-forest/

    ————————-
    Timeline!
    0:00 – Introduction
    2:02 – LeetCode 1678 – Goal Parser Interpretation
    6:04 – LeetCode 1436 – Destination City
    14:07 – LeetCode 1365 – How Many Numbers Are Smaller Than the Current Number
    33:13 – LeetCode 704 – Binary Search
    46:13 – LeetCode 409 – Longest Palindrome
    55:01 – LeetCode 1561 – Maximum Number of Coins You Can Get
    1:10:09 – LeetCode 1472 – Design Browser History
    1:22:32 – LeetCode 1110 – Delete Nodes And Return Forest
    1:38:23 – Conclusion & Announcement!

    ————————-
    Follow me on social media!
    Instagram | https://www.instagram.com/keithgalli/
    Twitter | https://twitter.com/keithgalli

    ————————-
    If you are curious to learn how I make my tutorials, check out this video: https://youtu.be/LEO4igyXbLs

    *I use affiliate links on the products that I recommend. I may earn a purchase commission or a referral bonus from the usage of these links.

    source

    Previous articleTop 50 Git Interview Questions and Answers | Git Interview Preparation | DevOps Training | Edureka
    Next articlePreparing for the Kubernetes Certifications – CKA & CKAD

    47 COMMENTS

    1. I applied for an SRE role with a company but they also wanted someone with some Python experience, other than building speech to text chatbots and local Jarvis virtual assistants I've never worked with Python on this type of level. I did horribly on the assessment which lead me to this video. Don't know why but I cannot wrap my head around this binary, data parsing or algorithm stuff with Python, it's next level type stuff… Big ups to all that understand this, I think I'll just stick with bash scripting and IAC- Orchestration/Automation and API's using Terraform, Ansible, Packer 😒

    2. Ok, this one works in my condition

      class Solution:
      def destCity(self, paths):#: List[List[str]]) -> str:
      going_count = {}
      for i in paths:
      [*cities] = i
      for j in range(len(cities)-1):
      going_count[cities[j]] = going_count.get(cities[j], 0) + 1
      going_count[cities[-1]] = 0

      print(going_count)

      dest = []

      for city in going_count:
      if going_count[city] == 0:
      dest.append(city)

      return dest

      paths = [['a', 'b', 'c', 'd'], ['a', 'c', 'e', 'f'], ['b', 'c', 'f'], ['d', 'j']]#, ['d', 'c']] # should not use dict

      solution = Solution()
      solution.destCity(paths)

    3. For Second Solution, I simply converted the nested list to the dictionary and iterated the dictionary to find the city which is not a key in that dictionary :

      cities = dict(cities)

      #{'London': 'New York', 'New York': 'Lima', 'Lima': 'Sao Paulo'}

      for city in cities.values() :

      if city not in cities.keys() :

      return city

    4. Hi, I tried the sharing between friends question but got no output
      data = [2, 4, 1, 2, 7, 8]
      Alice = 0
      Roso = 0
      Bob =0

      def split_data(data, Alice, Roso, Bob):
      share = len(data)//2
      while len(data) != 0:
      Alice += data[0]
      Roso += data[1]
      Bob += data[2]
      return Alice
      return Roso
      return Bob

    5. My solution was:
      If the second index of any pair does not exist as the source of any other pairs, then that's the city.

      def destCity(self, paths: List[List[str]]) -> str:

      src=[]

      dst=[]

      for x in paths:

      src.append(x[0])

      dst.append(x[1])

      for d in dst:

      if not (d in src):

      return d

    6. If the language is Python and you are allowed to 'use' Python, for question 704. Binary Search my initial thought was 'return nums.index(target)'. Why reinvent the wheel unless it was specified that the solution should be faster than the built-in function. Great video and this did definitely get the brain thinking about possible solutions and ways to improve them.

    7. interesting and tricky little prob:

      def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
      result = []
      for i in range(len(nums)):
      targets = [1 for (idx,x) in enumerate(nums) if idx != i and x < nums[i]]
      result.append(sum(targets))
      return result

    8. Love the video, but I dont think the longest palindrome problem is fully correct because if you have 2 instances of odd characters, you would need to only add the count of the largest number of a specific odd character. EX: "abbccccddeee" would need to ignore the odd "a" character but instead would include the 3 odd "e" characters.

    9. The very first problem of Goal Parser Interpretation, I made a mess.
      1. First I didn't read the question properly. My mind started thinking of different edge cases, wherein different words might be given as test inputs. (e.g. F()()t(ball), etc)
      2. I started looping over the string, I'm much habituated to loop over things which is the worst approach ever.
      3. I started trying to figure out to how to replace the single brackets without creating blank spaces.
      4. I never read the constraints properly.

      Thanks Keith for this video, it help me analyze my problems and I can start working on fixing those whilst attempting these interview questions.

    10. Great job Keith! Thanks a lot.
      In case someone finds the maxCoin solution too confusing with all those i and indexes, here is simpler one I came up with:
      def maxCoins(piles):

      sorted_piles = sorted(piles)

      # sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]

      my_score = 0

      while len(sorted_piles) > 0:

      del sorted_piles[-1] #deleting the biggest

      my_score += sorted_piles.pop(-1)

      del sorted_piles[0] #deleting the smallest

      return my_score

    11. For smallerNumbersThanCurrent() could you get rid of the if statement and just overwrite the dictionary values, because the last overwrite will be the correct one to save. Or does writing to a dictionary take more time than the if statement to check?

    12. Love your videos. Each time you upload a new one, its just like you read my mind and give me exactly what I need to become a data scientist. Please do a SQL tutorial or even better a "real world" example of how using SQL together with Python. Greetings from Sweden