Amazon Coding Interview Question - K Closest Points to the Origin
Skills:
Systems Design Basics80%
Key Takeaways
Solving Amazon coding interview questions like K Closest Points to the Origin using coding interview resources
Full Transcript
here's a coding interview problem from Amazon we have an array of two posts or an array of arrays of two items each this represents points on a two-dimensional space so for example this first item - 2 4 represents this point right here because the x-coordinate of this point is minus 2 and the y coordinate of this point is 4 and the problem is this given these points how would you find that K closest points to the origin and of course the origin is at 0 0 so for example if the given K is 2 how would you find the two closest points to the origin out of these points the two closest points here are minus 1 0 and 0 minus 2 so you should be able to print these points and try solving this problem in a more general way to practice pause the video right here and think about how you would solve the problem and then come back to the video the first natural step for solving this problem is to find the distance to each point from the origin and of course we can use the Pythagorean theorem to do that so to find the distance to for example at this point minus 2 4 we can just find the value of square root of minus 2 squared plus 4 squared which is equal to square root of 4 plus 16 which is square root of 20 so that's the distance from the origin to minus 2 4 and we can go through the same procedure for every other point to find the distance for every other point as well and after that you can just construct a new array with objects containing the coordinates of each point as well as the distance to each point from the origin and this new array might look like this I'm calling this new array points with D as in points with distance and the first object of this new array will correspond to the first item in the original array with the coordinate - - for and with this s square root of 20 of course and we're going to have the same type of object for every other point in the original points so the length of points with T the new array will be the same as the length of the original array points and you can use dictionaries or hash tables to store this information instead of using objects as well once you have this array all you need to do is you just need to find the K objects or the K points with the shortest distance so at this point this problem is actually equivalent to the problem of finding the K smallest elements in a given array of numbers for example if the given array is this array of integers and if the given K is 3 we want to be able to find the three smallest items out of this array which are 1 2 and there are right here now there are a few different approaches for solving this problem the simplest approach is the one in which you sort this array in an ascending order so that the smallest items come first and out of the sorted array take the K first items and that approach would take Big O of n log N in time if you use for example quick sort or merge sort another approach is using a variation of something called selection sort and I'm not going to go into detail here in this video but this approach would take big off and K in time now my personal favorite approach is the one in which we use a max-heap and this one would take big of k plus n minus k times log k in time let me explain how it works first of all let's quickly go over what a heap is in a sentence it is an efficient way to keep track of the largest value in the given collection of numbers suppose as an example you're given these three numbers 4 1 & 5 if you construct a max-heap with these three numbers you can sort of visualize it as a bag or a box of the three numbers and however many numbers you have in the max-heap retrieving the largest number out of it is very efficient in fact asking the heap what the largest number is without modifying the heap so just examining what the number is without retrieving the number itself will be done in Big O of one in time now there are a few other operations we're going to use on a heap for this particular problem the first one is create creating a heap a max-heap with K items takes Big O of K in time the second operation is replace so for example if we want to replace the current largest number in the given heap this one with a new number three then this heap will be rearranged so that the current largest number will be four and they will have one and three below it this operation takes Big O of log of K where K is the number of items in heap and the third operation is print off this print all the items in the given heap in the order that's not necessarily sorted so for example this operation on this particular heap might print four one and three and this operation takes Big O of K in time now how can we use a max-heap and those operations on it to find the K smallest items in a given array of numbers let's consider this array of numbers a game and let's say we want to find the three smallest items then the first step will be to construct a max-heap with the first K items or with the first three items after that for each item in the remainder of the array ask ourselves is this number smaller than the current largest number in the heap if it is smaller then replace it with a lakh current largest number so when we are examining this number right here to the heap will look like this and the current largest number has become four and when we're examining this number three four will be replaced wait three we're going to keep going like that and when we're examining this number ten because this is not smaller than the current largest number in the max-heap we'll just ignore that number and at the end of this process just print all items from this heap and we're done we have the K smallest items from the given array now let's quickly recap our entire strategy using pseudocode we're going to define our function closests which takes the given points and K for finding the K closest points to the origin and from this function we're not going to return anything but we're going to print the K closest point and in this function first of all using points create points with D for points with distance an array of objects containing the coordinates as well as the distance to each point from the origin after that using points with D the array of objects create a max-heap with the first K items and use the distance as the key when we create a max-heap let's just call this max heap MH after that run a for loop for each point in the remainder of the array and if this points distance is less than the current max of MH then replace mah is current max with P and once we're done with this for loop then just print all points in image and we're done now what about time complexity let's think about it line by line the first line here using points creating points with D takes Big O of n because we go through this array only once and creating a max-heap with K items as we discussed earlier takes Big O of K in time and here we're running n minus K loops in each loop replacing M H is current point with P takes Big O of log of K and so the runtime for this entire four loop will be big-oh of n minus K times log K in the worst case scenario after that printing all points in image will take Big O of K in time so adding them all up Big O of n plus big of K plus Big O of n minus K times log of K Plus speak of K we're going to have Big O of n plus n minus K times log K and that would be the runtime for this entire solution ok thanks a lot for watching this video if you liked this video I would also recommend my course on udemy you have an essential coding interview questions in which I cover eleven of the most essential coding interview questions to master with coding exercises in Python and Java in case you're interested in taking the course I put a discount code below in the description all right I'll see you in the next video
Original Description
Amazon coding interview question and answer.
If you liked this video, I would also recommend my Udemy course, "11 Essential Coding Interview Questions":
https://www.udemy.com/11-essential-coding-interview-questions/?couponCode=AMAZON2
(You'll get a discount through the link above.)
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from CS Dojo · CS Dojo · 18 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
▶
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
Longest Common Subsequence (Dynamic Programming)
CS Dojo
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
Coding Interview Question: Tower Hopper Problem
CS Dojo
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
How I Got an Internship at Microsoft
CS Dojo
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
Why I Left My $100,000+ Job at Google
CS Dojo
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
How I Learned to Code - and Got a Job at Google!
CS Dojo
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
What Is Dynamic Programming and How To Use It
CS Dojo
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
What Programming Language Should I Learn First?
CS Dojo
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
Using Boolean in Python (Python Tutorial #11)
CS Dojo
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
What Can You Do with Python? - The 3 Main Applications
CS Dojo
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
Google Coding Interview - Universal Value Tree Problem
CS Dojo
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
Google Onsite Interview Question #14: Design a High-Throughput Thread-Safe LRU Cache with TTL
Medium · Programming
The Problem With “Best Practices” in .NET Development
Medium · Programming
1.30 bytes/key: a compact string→id index in Rust
Medium · Python
La mentalidad para programar sistemas
Dev.to · Carlos Arturo Castaño G.
🎓
Tutor Explanation
DeepCamp AI