Amazon-Last 30 days Flashcards

sharing the last 30 days Medium, Hard problems

1
Q

Longest substring without repeating characters(Find the longest substring without repeating characters)

https://leetcode.com/problems/longest-substring-without-repeating-characters/solutions/5111376/video-3-ways-to-solve-this-question-sliding-window-set-hashing-and-the-last-position/?envType=company&envId=amazon&favoriteSlug=amazon-thirty-days

A

from collections import defaultdict
from typing import Dict
def longest_substring_without_repeating_characters(s:str) ->int:
longest = 0
l = 0
counter: Dict[str, int] = defaultdict(int)
for r in range(len(s)):
counter[s[r]] += 1
while counter[s[r]] > 1:
counter[s[l]] -= 1
l += 1
longest = max(longest, r- l + 1)
return longest

if__name__ == “__main__”:
s = input()
res = longest_substring_without_repeating_characters(s)
print(res)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

**Word Ladder

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

LRU cache

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Number of islands

A

class solution:
def numsislands(self, grid):
if not grid:
return 0
num_islands = 0

for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == “1”
self.dfs(grid, i, j)
num_islands += 1

return num_islands

def dfs(self, grid, r, c):
if (
r < 0
or c < 0
or r >= len(grid)
or c >= len(grid[0])
or grid[r][c] != “1”
):
return
grid[r][c] = “0”

      self.dfs(grid, r - 1, c)
			self.dfs(grid, r + 1, c)
			self.dfs(grid, r, c - 1)
			self.dfs(grid, r, c + 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Reorganise the string

A

two appraches we can solve this problem
1)counting and priority queue-
a) what is prority queue -priority quueue is always keeping the highest and lowest priority element on the top and allowing to access it in O(1) time
useful in scheduling , gredy algorithms, simulations where you repeatedly need the next most important item
b)advanatges of a priority queue -uses the binary heap for fast iteration and deletion insert - O(log n), remove the element - O(log n)

dynamically manintains the orders sort the elements every time you add or remove - the prority queue keeps the internal structure automaticaly
2)We can also use max heap for the solution. Maxheap does in log n times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Top k Frequent Elements

A

Heap takes k log(n) so we hav eto use the bucket sort to solve the problem

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Group Anagrams

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

**3Sum

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Rotting oranges

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Copy list Random pointer

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly