Skip to content

Commit

Permalink
Amazon SDE added
Browse files Browse the repository at this point in the history
  • Loading branch information
jayshah19949596 committed Dec 4, 2018
1 parent 195ffa0 commit 158ef43
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Amazon SDE - 1/README.md
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


24 changes: 24 additions & 0 deletions Amazon SDE - 1/kNearestPoints.py
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)
50 changes: 50 additions & 0 deletions Amazon SDE - 1/shortestPath.py
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)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
<th>Test</th>
</tr>

<tr>
<td> <a href="https://github.com/jayshah19949596/CodingInterviews/tree/master/Akuna%20Capital%20Junior%20Python%20Developer" target="_blank"> Akuna Capital </a> </td>
<td> SDE - 1</td>
<td> Dec 2018 </td>
<td> Amazon amcat</td>
</tr>

<tr>
<td> <a href="https://github.com/jayshah19949596/CodingInterviews/tree/master/Akuna%20Capital%20Junior%20Python%20Developer" target="_blank"> Akuna Capital </a> </td>
<td> Junior Python Developer</td>
Expand Down

0 comments on commit 158ef43

Please sign in to comment.