Welcome to the official KA Education RU-vid Channel!
Here, you'll find videos that help you learn the skills, tricks, and techniques you need to ace your technical interviews!
Topics covered include data structures/algorithms, system design, best software development practices, and interview questions!
Want more? Check out our website, where you can claim your FREE copy of our data structures crash course, see all our FULL-LENGTH courses, and even sign up for 1-1 tutoring!
This solution is failing for the edge case that I've created - [3, 2, 3, 1, 1, 1] For this, provided solution returns undefined. Need slight improvement in condition to fix this. Below one is working fine. let majorityElement = function (nums) { const ht = {}; for (const num of nums) { ht[num] = ht[num] + 1 || 1; } const isNumsOfEvenLength = nums.length % 2 === 0; const median = Math.floor(nums.length / 2); for (const key in ht) { if (isNumsOfEvenLength && ht[key] >= median) { return key; } else if (!isNumsOfEvenLength && ht[key] > median) { return key; } } };
Or you can simply use the below solution let majorityElement = function (nums) { const ht = {}; for (const num of nums) { ht[num] = ht[num] + 1 || 1; } const median = Math.ceil(nums.length / 2); for (const key in ht) { if (ht[key] >= median) { return key; } } }; console.log(majorityElement([3, 2, 3])); // 3 console.log(majorityElement([3, 2, 3, 1, 1, 1])); // 1
is there a way to replace zookeeper? why not just store a record of ranges used in a db or redis cache. since the operation of generating a new range happens once in a while. specifically, when a new server is being initialized and another when the range of a server rans out and need to fetch a new one
Why not just rely on a database cluster to generate a sequencial numeric id , and just persist the id and the long url ? So when a request comes in, we convert from base62 to decimal and find the record in the db. What is wrong of this idea ?
If in case anyone is interested, the O(1) space complexity requires some bit manipulation. Essentially if you XOR a number with itself, it returns zero, and if you XOR a number with zero, it returns the number itself. So if you had the following numbers in the list - 3, 3, 2, the XOR between 3 and 3 would return 0, and then XORing 0 with 2 would return 2. Hence if you did a XOR for all numbers in a list which contains 2 of each numbers except for the one number, then you'd get back the number which occurs once. The code is pretty straightforward since you only need to do a XOR for every number, and return the result. Code in Javascript - var singleNumber = function(nums) { let res = nums[0]; for (let i = 1; i<nums.length; i++) { res = res ^ nums[i]; } return res; };
Could you do more system design videos? This was super insightful and really well explained. Any of these would be so helpful to understand in your delivery and explanatory sttyle: Google Docs Twitter LeetCode API Rate Limiter BookMyShow Chat Application E-Commerce Portal Splitwise Vending Machine Google Autosuggest Uber Parking Lot Stock Exchange Logging System Authentication Service Either way, thanks for the video man!
base62 encoding can result in a string of any length. And we are not supposed to take first 7 chars to avoid collision. So that means we take what ever output we get from base64. This is what I want to hear from these videos, but believe me none of them emphasize on this. They just say blabla and use base62.
We are not encoding the original URL because as u said it would result in a string of any length. Instead we are encoding the numbers (0 - 3.5 trillion) which would not exceed 7 chars because 62^7 > 3.5 trillion.
If we add 10-15 bits at the end of counter number then it will increase the base62 output size and exceed the 7 character limit. So can you clarify how that addition of random string works?
TLDR: md5(“123”) vs md5(“123,xyz”) The original solution hashed the returned counter value, which is a simple increasing number. The resulting hash is then base62 encoded. The new solution takes the counter number and appends some extra characters at the end. This new string is then hashed and base62 encoded. In this way, the string sent into hash is not guessable. This works because the numeric portion is still guaranteed not to collide.
Even without appending extra digits, base62 does not guarantees 7 chars. That simply means, we are not confining ourselves to 7 chars. We probably need to tell the interviewer that we would get as small as possible hash but no guarantees. This is a possible tradeoff to avoid collision.
For caching, will it be better to have some kind of background job that populates the cache with the most popular URLs from the database? Else if you're always adding a URL to the cache, then it's no longer just the popular URLs but all of them (or at least the capacity of the cache).
Great video! But perhaps a simpler solution might look like this: var moveZeroes = function(nums) { for(let i = 0; i < nums.length; i++){ if(nums[i] === 0){ nums[nums.length - 1] = nums.splice(i,1) } } }; This will only require one loop. If anyone can see any issues with this solution at scale please let me know.
I did that and it passed most cases, but when it came to a really long array of length >14 or 15, for some reason it changed the last 4 numbers in the array to zero instead of what they actually were.