Solving Tesla's 2020 Most Asked Interview Question
ฝัง
- เผยแพร่เมื่อ 6 ส.ค. 2020
- 🎧 Join the community Discord: / discord
💰 Support me on Patreon: / michaelmuinos
🔗Follow me on LinkedIn: / michael-muinos
📂Follow me on Github: github.com/MichaelMuinos
Check out my interview prep platform for learning the patterns!
📢 Interview Prep Platform: algoswithmichael.com
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
/ lakeyinspired
/ @lakeyinspired - วิทยาศาสตร์และเทคโนโลยี
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.
Awesome simple solution.
A good approach. Enjoyed the video!
Stay safe!
Thank you, same to you!
so brilliant! what an easier method to use! tq so much 👍🏻
so understandable!
Nicely Described...Thank you
You're most welcome
Thanks. Awesome as always
No problem 👍
i can’t believe i knew the answer outright for a Tesla question 😱
Great sir ,Thank you very much sir
Anytime!
Teslas most asked question..... "You cool with panel gaps?"
LOL
Why you are too good ?
1) you talk first about approach in a pretty easy way
2) MOST IMPORTANT, you explain the TIME COMPLEXITY 💡
Haha I'm glad you like the format. Thank you for watching, more videos to come!
@@AlgosWithMichael 👍
Hey can you add some more videos in this playlist. TIA
Definitely
here count array can't be initialized
what are the initial values in count
zeros
if your dividing by 2 shouldnt it log n base 2?
The division portion takes constant time, but looping over all characters is going to take linear time.
I am mad on the premise I would've done something longer than this....
This is why I prefer Object-Oriented programming over algorithms!
Haha relatable
Thanks man
No prob!
what languages should I learn!?!!?!?!? I have HTML, CSS, JS, SQL, React
Learn any languages you are interested in learning. Can't go wrong with Python though, lots of things you can do with it
Jesus... I switching from c# to c++
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
}
Yep, map approach is just fine! Both ways to solve it have the same time / space.
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([
text_counter[ch]//subtext_counter[ch] for ch in subtext
])
Bro I don’t even understand the question haha
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"
.