Solving Tesla's 2020 Most Asked Interview Question

preview_player
Показать описание

Check out my interview prep platform for learning the patterns!

In this video, I go over a full solution for solving Tesla's most commonly asked interview coding question in 2020. The problem is called "Maximum Number of Balloons" and was found on the LeetCode platform.

For this problem, we need to find the max number of "balloon" words we can create by just using the letters in our input string. To solve this, we need to get a count of all characters in our string. We could use a HashMap data structure to do this; however, a better way is to have an integer array of size 26. We use 26 because our input will always only be lowercase letters. Each index will map to a unique lowercase letter starting with lowercase 'a' at index 0.

With these counts, we calculate the minimum count at the indices containing the characters 'b', 'a', 'l', 'o', and 'n'. We use these characters because those are what make up the word "balloon".

Our time complexity is going to be big oh N where N is the number of character in our input string. Our space complexity is going to be constant -- since the array we initialize will always be of size 26, it won't add extra memory.

----------------------------------------------------
Better Days by Lakey Inspired
Рекомендации по теме
Комментарии
Автор

I think we can have an integer of size 5. Our only interest is in the letter 'b', 'a', 'l', 'o', 'n'. We can ignore the other ones and do the min on that array.

webdeveloper
Автор

Awesome simple solution.
A good approach. Enjoyed the video!
Stay safe!

yousufbaig
Автор

i can’t believe i knew the answer outright for a Tesla question 😱

TSAMikeyo
Автор

so brilliant! what an easier method to use! tq so much 👍🏻

SPMenOfficial
Автор

here count array can't be initialized
what are the initial values in count

ncc-cu
Автор

Why you are too good ?
1) you talk first about approach in a pretty easy way
2) MOST IMPORTANT, you explain the TIME COMPLEXITY 💡

codestorywithMIK
Автор

Teslas most asked "You cool with panel gaps?"

cbjueueiwyru
Автор

if your dividing by 2 shouldnt it log n base 2?

Bandz-rlqk
Автор

Hey can you add some more videos in this playlist. TIA

samikshadharmadhikari
Автор

I am mad on the premise I would've done something longer than this....

This is why I prefer Object-Oriented programming over algorithms!

randommindz
Автор

what languages should I learn!?!!?!?!? I have HTML, CSS, JS, SQL, React

sabertoothwallaby
Автор

read the question again
it tells you, you can only use once the chars in text
so when you count them .. you already used them once! !
it doesn't tell you "use once to form the word balloon" . so I assume use once to process the data "text"
.

henrifritsmaarseveen
Автор

My approach the map approach, should work for any input unicode string, should still be order N, although not quite as efficient as doing math. in Swift, it is not as simple to map to ascii: 

func maxNumberOfBalloons(_ text: String) -> Int {
let balloon = "balloon"
var chars: [Character: Int] = [:]
for char in text {
if balloon.contains(char) {
chars[char, default: 0] += 1
}
}
var count = 0
while true {
for char in balloon {
if chars[char, default: 0] == 0 {
return count
}
chars[char, default: 0] -= 1
}
count += 1
}
return count
}

tomladdus
Автор

Bro I don’t even understand the question haha

championsofodin
Автор

Here's an easy solution in Python3 which is generic (not restricted to just balloons).

from collections import Counter

class Solution:
def maxNumberOfBalloons(self, text: str) -> int:
return self.max_number_of_subtext(text, "balloon")


def max_number_of_subtext(self, text: str, subtext: str) -> int:
text_counter = Counter(text)
subtext_counter = Counter(subtext)

return min([
for ch in subtext
])

NBRIUM