forked from Sindurav/CodingInterviews
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
195ffa0
commit 158ef43
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Amazon amcat | ||
|
||
## Online Assessment Overview | ||
The assessment consists of these components: | ||
- a coding challenge with two scenarios (up to 90 min) | ||
- a "describe your approach" section to discuss your coding solutions (up to 15 min) | ||
- a work style survey (up to 15 min) | ||
- a feedback survey (5 min) | ||
|
||
I am not disclosing the exact questions due to NDA. | ||
My code has passed all the test cases of Amazon amcat. | ||
The code provided over here is not exactly the same code that I submitted because I have rewritten the code. | ||
I assure that the solution provided is correct. | ||
|
||
|
||
|
||
## 1. Nearest delivery stations | ||
|
||
- Given an array of points in 2d space and a goal point, write a function that returns the top k closest points to the goal point. | ||
|
||
## 2. Shortest Path | ||
|
||
- Given a MxN matrix where each element can either be 0 or 1. | ||
- We need to find the shortest path between a given source cell to a destination cell. | ||
- The path can only be created out of a cell if its value is 1. | ||
- You will be provided with start and goal locations | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import math | ||
|
||
|
||
def compute_euclidean_distance(x1, y1, x2, y2): | ||
return math.sqrt(((x1-x2)**2)+((y1-y2)**2)) | ||
|
||
|
||
def nearest_neighbor(points, goal_point, k): | ||
distances = [] | ||
|
||
for point in points: | ||
distance = compute_euclidean_distance(point[0], point[1], goal_point[0], goal_point[1]) | ||
distances.append([point, distance]) | ||
else: | ||
distances.sort(key=lambda element: element[-1]) | ||
|
||
closest_neighbors = [distances[i][0] for i in range(0, k)] | ||
return closest_neighbors | ||
|
||
|
||
points_array = [[6, 3], [2, 1], [5, 2], [3, 2], [9, 0]] | ||
goal = [3, 2] | ||
neighbors = nearest_neighbor(points_array, goal, 3) | ||
print(neighbors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from collections import deque | ||
|
||
|
||
def is_valid(x, y, n, m, matrix, visited): | ||
if (0 <= x < n) and (0 <= y < m) and (matrix[x][y] == 1) and ((x, y) not in visited): | ||
visited.add((x, y)) | ||
return True | ||
return False | ||
|
||
|
||
def shortest_path(matrix, start, goal): | ||
neighbors = ((0, 1), (0, -1), (1, 0), (-1, 0)) | ||
|
||
visited = set([]) | ||
visited.add((start[0], start[1])) | ||
|
||
queue = deque() | ||
queue.appendleft([start[0], start[1], 0, [start]]) | ||
|
||
while queue: | ||
|
||
x, y, step, path = queue.pop() | ||
|
||
if [x, y] == goal: | ||
return path | ||
|
||
for i in range(len(neighbors)): | ||
new_x = x+neighbors[i][0] | ||
new_y = y+neighbors[i][1] | ||
if is_valid(new_x, new_y, len(matrix), len(matrix[0]), matrix, visited): | ||
queue.appendleft((new_x, new_y, step+1, path+[[new_x, new_y]])) | ||
|
||
return [] | ||
|
||
|
||
mat = [[1, 0, 1, 1, 1, 1, 0, 1, 1, 1], | ||
[1, 0, 1, 0, 1, 1, 1, 0, 1, 1], | ||
[1, 1, 1, 0, 1, 1, 0, 1, 0, 1], | ||
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1], | ||
[1, 1, 1, 0, 1, 1, 1, 1, 1, 0], | ||
[1, 0, 1, 1, 1, 1, 0, 1, 0, 0], | ||
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1], | ||
[1, 0, 1, 1, 1, 1, 0, 1, 1, 1], | ||
[1, 1, 0, 0, 0, 0, 1, 0, 0, 1]] | ||
|
||
|
||
a = [0, 0] | ||
b = [5, 7] | ||
result_path = shortest_path(mat, a, b) | ||
print(result_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters