Amazon Coding Interview Question - K Closest Points to the Origin

CS Dojo · Intermediate ·🏗️ Systems Design & Architecture ·8y ago

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 4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
4 Hacks for Finding the Optimal Answer in Coding Interview QUICKLY!
CS Dojo
2 Dynamic Programming Tutorial with Fibonacci Sequence
Dynamic Programming Tutorial with Fibonacci Sequence
CS Dojo
3 Kadane's Algorithm to Maximum Sum Subarray Problem
Kadane's Algorithm to Maximum Sum Subarray Problem
CS Dojo
4 Longest Common Subsequence (Dynamic Programming)
Longest Common Subsequence (Dynamic Programming)
CS Dojo
5 0-1 Knapsack Problem (Dynamic Programming)
0-1 Knapsack Problem (Dynamic Programming)
CS Dojo
6 Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
Amazon Coding Interview: Count Negative Integers in Row/Column-Wise Sorted Matrix
CS Dojo
7 Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
Microsoft Coding Interview Question and Answer: Lowest Common Ancestor
CS Dojo
8 Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
Learn Counting Sort Algorithm in LESS THAN 6 MINUTES!
CS Dojo
9 Radix Sort Algorithm Introduction in 5 Minutes
Radix Sort Algorithm Introduction in 5 Minutes
CS Dojo
10 Coding Interview Question and Answer: Longest Consecutive Characters
Coding Interview Question and Answer: Longest Consecutive Characters
CS Dojo
11 Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
Coding Interview: Can You RANDOMLY Reorder Array in O(N)?
CS Dojo
12 Coding Interview Question: Tower Hopper Problem
Coding Interview Question: Tower Hopper Problem
CS Dojo
13 Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
Problem Solving Technique #1 for Coding Interviews with Google, Amazon, Microsoft, Facebook, etc.
CS Dojo
14 Google Coding Interview Question and Answer #1: First Recurring Character
Google Coding Interview Question and Answer #1: First Recurring Character
CS Dojo
15 Facebook Coding Interview Question and Answer #1: All Subsets of a Set
Facebook Coding Interview Question and Answer #1: All Subsets of a Set
CS Dojo
16 Think you're not smart enough to work at Google? Well, think again.
Think you're not smart enough to work at Google? Well, think again.
CS Dojo
17 How to Crack a Google Coding Interview - An Ex-Googler’s Guide
How to Crack a Google Coding Interview - An Ex-Googler’s Guide
CS Dojo
Amazon Coding Interview Question - K Closest Points to the Origin
Amazon Coding Interview Question - K Closest Points to the Origin
CS Dojo
19 How I Got an Internship at Microsoft
How I Got an Internship at Microsoft
CS Dojo
20 How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
How I Got a Job at Google as a Software Engineer (without a Computer Science Degree!)
CS Dojo
21 Why I Left My $100,000+ Job at Google
Why I Left My $100,000+ Job at Google
CS Dojo
22 Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
Top 5 Programming Languages to Learn to Get a Job at Google, Facebook, Microsoft, etc.
CS Dojo
23 How I Learned to Code - and Got a Job at Google!
How I Learned to Code - and Got a Job at Google!
CS Dojo
24 Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
Why I Left Google To Be A YouTuber FULL-TIME (and NOT part-time!)
CS Dojo
25 What Is Dynamic Programming and How To Use It
What Is Dynamic Programming and How To Use It
CS Dojo
26 Python Tutorial for Absolute Beginners #1 - What Are Variables?
Python Tutorial for Absolute Beginners #1 - What Are Variables?
CS Dojo
27 What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
What's It Really Like To Intern At Google? (LIVE with a former Google software engineer intern)
CS Dojo
28 How to Use If Else Statements in Python (Python Tutorial #2)
How to Use If Else Statements in Python (Python Tutorial #2)
CS Dojo
29 Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
Dynamic Programming Interview Question #1 - Find Sets Of Numbers That Add Up To 16
CS Dojo
30 How To Use Functions In Python (Python Tutorial #3)
How To Use Functions In Python (Python Tutorial #3)
CS Dojo
31 What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
What’s It Like To Be A Program Manager Intern At Microsoft? (LIVE with a former Microsoft intern)
CS Dojo
32 Introduction To Lists In Python (Python Tutorial #4)
Introduction To Lists In Python (Python Tutorial #4)
CS Dojo
33 Introduction to For Loops in Python (Python Tutorial #5)
Introduction to For Loops in Python (Python Tutorial #5)
CS Dojo
34 What Programming Language Should I Learn First?
What Programming Language Should I Learn First?
CS Dojo
35 What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
What Is Competitive Programming and How To Prepare For It (LIVE with Gaurav Sen)
CS Dojo
36 While Loops and The Break Statement in Python (Python Tutorial #6)
While Loops and The Break Statement in Python (Python Tutorial #6)
CS Dojo
37 More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
More About For Loops in Python & Solutions to the Last 2 Problems (Python Tutorial #7)
CS Dojo
38 How to Learn to Code - Best Resources, How to Choose a Project, and more!
How to Learn to Code - Best Resources, How to Choose a Project, and more!
CS Dojo
39 How To Use Dictionaries In Python (Python Tutorial #8)
How To Use Dictionaries In Python (Python Tutorial #8)
CS Dojo
40 Data Structures & Algorithms #1 - What Are Data Structures?
Data Structures & Algorithms #1 - What Are Data Structures?
CS Dojo
41 An Overview of Arrays and Memory (Data Structures & Algorithms #2)
An Overview of Arrays and Memory (Data Structures & Algorithms #2)
CS Dojo
42 Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3)
CS Dojo
43 Classes and Objects with Python - Part 1 (Python Tutorial #9)
Classes and Objects with Python - Part 1 (Python Tutorial #9)
CS Dojo
44 Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
Introduction to Classes and Objects - Part 2 (Data Structures & Algorithms #4)
CS Dojo
45 Classes and Objects with Python - Part 2 (Python Tutorial #10)
Classes and Objects with Python - Part 2 (Python Tutorial #10)
CS Dojo
46 Introduction to Linked Lists (Data Structures & Algorithms #5)
Introduction to Linked Lists (Data Structures & Algorithms #5)
CS Dojo
47 Introduction to Recursion (Data Structures & Algorithms #6)
Introduction to Recursion (Data Structures & Algorithms #6)
CS Dojo
48 Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
Introduction to Big O Notation and Time Complexity (Data Structures & Algorithms #7)
CS Dojo
49 Amazon Coding Interview Question - Recursive Staircase Problem
Amazon Coding Interview Question - Recursive Staircase Problem
CS Dojo
50 Using Boolean in Python (Python Tutorial #11)
Using Boolean in Python (Python Tutorial #11)
CS Dojo
51 Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
Intro to Data Analysis / Visualization with Python, Matplotlib and Pandas | Matplotlib Tutorial
CS Dojo
52 What Can You Do with Python? - The 3 Main Applications
What Can You Do with Python? - The 3 Main Applications
CS Dojo
53 Facebook Coding Interview Question - How Many Ways to Decode This Message?
Facebook Coding Interview Question - How Many Ways to Decode This Message?
CS Dojo
54 List Comprehension Basics with Python (Python Tutorial #12)
List Comprehension Basics with Python (Python Tutorial #12)
CS Dojo
55 How To Use Sets in Python (Python Tutorial #13)
How To Use Sets in Python (Python Tutorial #13)
CS Dojo
56 Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
Python books for beginners? What Python projects to work on? | 2 Python Beginner FAQ’s!
CS Dojo
57 Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
Resources for Learning Data Structures and Algorithms (Data Structures & Algorithms #8)
CS Dojo
58 6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
6 Python Exercise Problems for Beginners - from CodingBat (Python Tutorial #14)
CS Dojo
59 Google Coding Interview - Universal Value Tree Problem
Google Coding Interview - Universal Value Tree Problem
CS Dojo
60 Best laptops for programming? How to get a job at Google? - And other FAQ’s!
Best laptops for programming? How to get a job at Google? - And other FAQ’s!
CS Dojo

Related Reads

Up next
8. Steps in Supply Chain Management from Logistics & Supply Chain Management Subject
Devika's Commerce & Management Academy
Watch →