DOT PRODUCT OF TWO SPARSE VECTORS - 3 SOLUTIONS EXPLAINED [PYTHON]

  Рет қаралды 10,690

Cracking FAANG

Cracking FAANG

Күн бұрын

Пікірлер: 43
@landocodes
@landocodes 9 ай бұрын
The "rambling" at the end was super helpful. This problem seemed way too easy so I figured I was doing it wrong, and ofc I was. Great explanation as always.
@aaron10k
@aaron10k 9 ай бұрын
grinding these for my meta interview they're so good
@Rhythm31
@Rhythm31 9 ай бұрын
did you give it? I have my final rounds in a week
@annlaosun6260
@annlaosun6260 5 ай бұрын
@@Rhythm31 did you get it? i have my interview in a couple weeks
@TheSmashten
@TheSmashten Ай бұрын
How did it go? What questions were you asked
@rachnaramkumar
@rachnaramkumar 2 жыл бұрын
Got this question for my Meta interview today! Thank you so much for your videos! My interviewer ended up accepting my Tuple Solution! Looking forward to more videos :) Keep up the amazing work!
@crackfaang
@crackfaang 2 жыл бұрын
Happy this was able to help you! Hopefully you passed the on-site and will receive an offer
@sunnygoswami5358
@sunnygoswami5358 2 жыл бұрын
Congrats on getting the solution accepted! What is the position you're interviewing for? Thanks!
@3rd_iimpact
@3rd_iimpact 11 ай бұрын
Did you get the follow-up question? If so, how did you explain?
@SuperAce780
@SuperAce780 10 ай бұрын
this is the best channel ever. helped me ace my meta interviews and get a full time offer! thank you
@crackfaang
@crackfaang 10 ай бұрын
Very happy to hear that mate! Congratulations on the offer and hopefully you got a big sign on bonus and large RSU grant :)
@roywastaken
@roywastaken Жыл бұрын
GOAT channel bro, come back!!!
@pahul79
@pahul79 Жыл бұрын
hey man i just discovered your channel. I have to say, im very surprised you havent blown up. even though the view count is low, no disrespect intended, i have to so your videos are top notch. Thank you very much for putting so much effort into these videos to help the public as myself.
@adityadn95
@adityadn95 2 ай бұрын
Do we really need binary search approach for the follow up? Can we not iterate over the sparse vector and directly access the corresponding indices in the dense vector?
@mohammadkareem1187
@mohammadkareem1187 2 жыл бұрын
Thanks for the great video, however the index pair solution might be acceptable in the interview, but in case one parse vetoer is extremely large, this is not going to be efficient, instead, one need to find the matching index in the long vector via binary search, which will be the best solution in terms of time complexity. You can find some solution examples in the discussion part of question in Leetcode :)
@crackfaang
@crackfaang 2 жыл бұрын
It’s not that simple. Who said time complexity is all we want to optimize for? What if we don’t care about time and space is the most important factor? This is one of those annoying questions where there is no “best” answer it it depends on what the interviewer is looking for. At the end of the day, interviewers aren’t really interested so much in the solution you choose for this problem, it’s more how to evaluate the pros/cons of each one within the constraints of the problem that you establish with the interviewer.
@mohammadkareem1187
@mohammadkareem1187 2 жыл бұрын
@@crackfaang I agree in general that the solution should be based on the constraints of the problem, however in this particular problem where you have SPARSE vectors, the whole idea is how to efficiently do the computation without going through redundant process of multiplying zeros (0 x 0), so the point is how to quickly find those common index non-zero elements. The target here is definitely time complexity and not space complexity, otherwise the naive dot production of two vectors is of O(1) in space complexity.
@crackfaang
@crackfaang 2 жыл бұрын
@@mohammadkareem1187 You're completely missing the point of this question. At the end of the day, the interviewers don't truly care about the minutia of your solution. This question is more about the discussion of the pros/cons to each different approach at the end of which the interviewer will usually say "okay code X solution". Whether they want binary search, tuple, dictionary approach, it doesn't matter: they want to see if you are able to clearly and concisely enumerate the benefits and drawbacks of the proposed solutions, much like you would in a real life scenario. I'm sure you're smart enough to know that and are simply arguing for the sake of being a smart ass on the internet. At the end of the day, solve it how you see fit. The purpose of the video was to establish 3 potential solutions and discuss their pros/cons and things the interviewer may poke holes in each approach. I'm sure there's some ultra optimal way using some crazy math to do this problem as well. Doesn't matter. Best of luck with the interview prep my G
@mohammadkareem1187
@mohammadkareem1187 2 жыл бұрын
@@crackfaang Bro we are at the same page and I fully agree with every single word you are saying. All I am saying is to be prepared in case the interviewer challenged your proposed solution and be aware of other solutions with less time / space complexity. All the best.
@segue97
@segue97 2 ай бұрын
@@mohammadkareem1187 In a case where you're give a sparse matrix with over 1x1e10 zeroes with limited memory, optimising for space complexity will be more relevant.
@awa8766
@awa8766 2 жыл бұрын
Thanks for that, however, I have a quick question regarding the syntax. I don't understand how at lines 16-18, initializing with self.nums and vec.nums is creating a difference. I thought both cases would be vec.nums since the dotproduct function is receiving an input that is passed to the __init__, preprocessed, then the operations from lines 16 onwards proceed. Initially, I thought of writing it as follows: while i < len(self.nums) and j < len(self.nums): i_idx, i_num = self.nums[i] j_idx, j_num = self.nums[j] but obviously, I'm getting duplicate numbers so it doesn't work. I'm not too sure how the way you wrote it is generating different results. Thanks in advance!
@crackfaang
@crackfaang 2 жыл бұрын
The “nums” part is an attribute on the SparseVector class. Basically the question passes you another SparseVector class to do the dot product with. So self.nums refers to the current SparseVector and it’s numbers and the vec.nums refers to the other sparse vector class. If you look at the function signature for the dot product function you’ll see that you get passed in the other vector
@l.k3072
@l.k3072 2 жыл бұрын
One of the best explanation channel
@crackfaang
@crackfaang 2 жыл бұрын
Thanks for the kind words! Make sure to subscribe to not miss future videos
@boyun7532
@boyun7532 2 жыл бұрын
Thank you so much for the video! can you elaborate a bit more on what a bad hash map function may look like? just looking for reasoning behind using tuple over hash in case getting asked
@crackfaang
@crackfaang 2 жыл бұрын
Thanks the kind words! I suppose this is more of a theoretical question as all languages are going to have optimized hash functions but one that is suboptimal would be one which has a lot of collisions when trying to hash values
@davidmwangi4312
@davidmwangi4312 5 ай бұрын
Super helpful as I prepare for my upcoming Meta Interview
@3rd_iimpact
@3rd_iimpact 9 ай бұрын
Good stuff. Could you please explain more into detail why 'j' is moved up instead of 'i' when i_idx > j_idx (line 20)? Also, could you please go more in detail about the follow up question and using the binary search?
@cheesehead6982
@cheesehead6982 2 жыл бұрын
EDIT: nvm - I see that we don't always advance N and M by one each iteration, N may get ahead of M, etc... How is this O(N+M) time for dot product? The loop simply iterates once through N items, sure we access nth and mth item at the same time, but array access is constant, no? It seems like this would just be O(N) O(N+M) would entail iterating through all N items, then a second loop that iterates through all M items...
@hemanthpalle259
@hemanthpalle259 Жыл бұрын
I think N and M are the number of non-zero elements.
@Rppiano
@Rppiano 6 ай бұрын
3 solutions: 1. Brute force 2. HashMap 3. Two pointers + HashMap
@solar679
@solar679 2 жыл бұрын
God bless you !!!!!!!!!!!!!!
@crackfaang
@crackfaang 2 жыл бұрын
Make sure to subscribe for more content and let me know if there’s any videos you’d like me to make!
@solar679
@solar679 2 жыл бұрын
​@@crackfaang thanks for the response. I was a little bit confused on why the first implementation was inefficient. Can you please explain again?
@aaron10k
@aaron10k 9 ай бұрын
isn't the space complexity for the dotProduct function constant?
@segue97
@segue97 2 ай бұрын
love the honesty to know he was handwaving. this guy is either dissing himself, leetcode or subscribers 😂
@rishab9293
@rishab9293 Ай бұрын
@Cracking FAANG Thank you! I was wondering if we could mention in the follow-up that I will perform a binary search on non-sparse vectors to find the non-zero index from the sparse vector. This will translate our dotProduct function's time complexity to 𝑂 ( min ⁡ ( 𝑚 , 𝑛 ) ⋅ log ⁡ ( max ⁡ ( 𝑚 , 𝑛 ) ) ) O(min(m,n)⋅log(max(m,n))) and space complexity to 𝑂 ( 1 ) O(1). Do you think we are expected to implement the follow-up question or just verbal conversations?
@ayoubalem865
@ayoubalem865 2 жыл бұрын
Keep Going ! By time you will gain more followers you are doing a great work !
@dARKf3n1Xx
@dARKf3n1Xx 2 жыл бұрын
It's amazing how at 2x every coding video still makes sense XD
@pat777b
@pat777b 6 ай бұрын
wow, my solution was kind of similar to your enumerate solution. Is there a downside to me using only lists instead of enumerate? Also, did you like the extra stuff I did in my while loop? I think it makes it faster overall. I do realize it costs O(1) time (within the O(n+m) loop) to do the extra checks but it also saves time if we exit the while loop earlier. class SparseVector: def __init__(self, nums: List[int]): self.storage = [] n = len(nums) for i in range(n): if nums[i] == 0: continue else: self.storage.append([i,nums[i]]) # Return the dotProduct of two sparse vectors def dotProduct(self, vec: 'SparseVector') -> int: ans = 0 s = self.storage slength = len(s) v = vec.storage vlength = len(v) i = 0 j = 0 while i < slength and j < vlength: if s[slength - 1][0] < v[j][0]: return ans elif v[vlength - 1][0] < s[i][0]: return ans elif s[i][0] == v[j][0]: ans += s[i][1]*v[j][1] i += 1 j += 1 elif s[i][0] > v[j][0]: j += 1 else: i += 1 return ans
@sudharshanchakravarthy7199
@sudharshanchakravarthy7199 Жыл бұрын
Amazing!
@dnm9931
@dnm9931 4 ай бұрын
Thanks a lot man
@jobhunter-j7d
@jobhunter-j7d Жыл бұрын
int dotProduct(vector &v1, vector &v2){ vector m1; vector m2; for(int i = 0; i < v1.size(); i++){ if(v1[i]) m1.push_back({i, v1[i]}); if(v2[i]) m2.push_back({i, v2[i]}); } int l = 0, r = 0, ans = 0; while(l < m1.size() && r < m2.size()){ if(m1[l].first == m2[r].first){ ans += m1[l].second * m2[r].second; l++; r++; } else if(m1[l].first < m2[r].first){ l++; } else{ r++; } } return ans; } v1 and v2 are passed as the arrays
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 12 М.
PRODUCT OF TWO RUN-LENGTH ENCODED ARRAYS | PYTHON | LEETCODE # 1868
16:24
Мама у нас строгая
00:20
VAVAN
Рет қаралды 11 МЛН
Vectors & Dot Product • Math for Game Devs [Part 1]
3:16:28
Freya Holmér
Рет қаралды 840 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 303 М.
1570. Dot Product of Two Sparse Vectors - Facebook Interview Question
5:59
Kth LARGEST ELEMENT IN AN ARRAY - SOLUTION EXPLAINED [PYTHON]
12:55
Cracking FAANG
Рет қаралды 6 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE II | PYTHON | LEETCODE 1644
20:46
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
I never understood why you can't go faster than light - until now!
16:40
FloatHeadPhysics
Рет қаралды 4 МЛН
MAKING A LARGE ISLAND | LEETCODE # 827 | PYTHON SOLUTION
22:09
Cracking FAANG
Рет қаралды 8 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.