Skip to content

Commit 3b51c51

Browse files
committed
Add 2453
1 parent 5b63c71 commit 3b51c51

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
def minSumDistancesToWarehouses(nums):
3+
nums.sort()
4+
best_split = 0
5+
max_diff = 0
6+
for i in range(1, len(nums)):
7+
if nums[i] - nums[i - 1] > max_diff:
8+
best_split = i
9+
max_diff = nums[i] - nums[i - 1]
10+
11+
first_half = nums[:best_split]
12+
second_half = nums[best_split:]
13+
14+
first_centroid = round(sum(first_half) / len(first_half))
15+
second_centroid = round(sum(second_half) / len(second_half))
16+
17+
total_dist = 0
18+
for num in nums:
19+
total_dist += min(abs(num - first_centroid), abs(num - second_centroid))
20+
return total_dist
21+
22+
print(minSumDistancesToWarehouses([0, 1, 2,2,2,2,4,4,8,9,9,10,10,11,13,14]))

2024/meta/Sep-screen-1.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#Question 1. Given a binary tree root node, write a function that returns the tree diameter
2+
#definition: diameter of a tree is the number of nodes on the longest path between any two nodes
3+
#example
4+
1
5+
/ \
6+
2 3
7+
/ / \
8+
4 5 6
9+
\
10+
7
11+
12+
13+
1
14+
\
15+
3
16+
/ \
17+
5 6
18+
\
19+
7
20+
21+
#ans = 6
22+
23+
def solution(root):
24+
res = 0
25+
26+
def dfs(node):
27+
nonlocal res
28+
if not node:
29+
return 0
30+
leftres = dfs(node.left)
31+
rightres = dfs(node.right)
32+
s = leftres + rightres + 1
33+
res = max(res, s)
34+
35+
return 1+max(leftres, rightres)
36+
37+
dfs(root)
38+
return res
39+
40+
41+
dfs(1): res = 1 + 2 + 3 = 6, => 1 + 3 = 4
42+
- dfs(2): res = 1 + 1 + 0 = 2, => 2
43+
- dfs(4) => 1 + 0 = 1, res = 1
44+
- dfs(None) => 0
45+
46+
- dfs(3): res = 1 + 1 + 2 = 4, => 3
47+
- dfs(5) => 1
48+
- dfs(6) => 2
49+
- dfs(7) => 1
50+
51+
52+
#Question 2. Given a matrix of integers print out its values along the diagonals that move in the top right to bottom left direction. Each diagonal goes down and to the left as shown in the example. There should be newlines between each diagonal.
53+
#example
54+
[[1, 2, 3, 4],
55+
[5, 6, 7, 8],
56+
[9, 10, 11, 12]]
57+
58+
1 (0, 0) sum = 0
59+
2 5 (0, 1), (1, 0) sum = 1
60+
3 6 9 (0, 2), (1,1) (2, 0) = sum = 2
61+
4 7 10 sum = 4
62+
8 11 sum = 5
63+
12 = sum = 6
64+
65+
66+
67+
3 6 9 (0, 2), (1,1) (2, 0) = sum = 2
68+
i, j
69+
0, 2
70+
1, 1
71+
2, 0
72+
73+
74+
for i in range(m):
75+
for j in range(n):
76+
77+
78+
# for s in range(m+n):
79+
# for i in range(m):
80+
# for j in range():
81+
def solution(matrix):
82+
rows, cols = len(matrix), len(matrix[0])
83+
84+
for col in range(cols):
85+
i, j = 0, col
86+
87+
while i < rows and j >= 0:
88+
print(matrix[i][j], end=' ')
89+
i += 1
90+
j -= 1
91+
92+
93+
for row in range(1, rows):
94+
i, j = row, cols-1
95+
96+
while i < rows and j >= 0:
97+
print(matrix[i][j], end=' ')
98+
i += 1 # same, move bottom left
99+
j -= 1
100+
101+
solution(
102+
[[1, 2, 3, 4],
103+
[5, 6, 7, 8],
104+
[9, 10, 11, 12]]
105+
)
106+
107+
rows = 3, cols = 4
108+
109+
col = 0
110+
i, j = 0, 0
111+
print 1
112+
113+
col = 1
114+
i, j = 0, 1
115+
print(2), i+=1 = 1, j -= 1, 0
116+
i, j = 1, 0
117+
print(5)
118+
119+
120+
col = 2
121+
i, j = 0, 2
122+
print(3),
123+
i, j = 1, 1
124+
print(6)
125+
i, j = 2, 0
126+
print(9)
127+
128+
129+
130+
col = 3
131+
i, j = 0, 3
132+
print(4),
133+
i, j = 1, 2
134+
print(7)
135+
i, j = 3, 0
136+
print(10)
137+
138+
139+
row = 1
140+
i, j = 1, 3
141+
print(8),
142+
i, j = 2, 2
143+
print(11)
144+
145+
row = 2
146+
i, j = 2, 3
147+
print(12),

2024/meta/system-design-prep.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
How to answer in a system design interview?
2+
1. For a Read-Heavy System - Consider using a Cache.
3+
2. For a Write-Heavy System - Use Message Queues for async processing
4+
3. For a Low Latency Requirement - Consider using a Cache and CDN.
5+
4. Need Atomicity, Consistency, Isolation, Durability Compliant DB - Go for RDBMS/SQL Database.
6+
5. Have unstructured data - Go for NoSQL Database.
7+
6. Have Complex Data (Videos, Images, Files) - Go for Blob/Object storage.
8+
7. Complex Pre-computation - Use Message Queue & Cache.
9+
8. High-Volume Data Search - Consider search index, tries or search engine.
10+
9. Scaling SQL Database - Implement Database Sharding..google  и
11+
10. High AvaPilability, Performance, & Throughput - Use a Load Balancer.
12+
11. Global Data Delivery - Consider using a CDN.
13+
12. Graph Data (data with nodes, edges, and relationships) - Utilize Graph Database.
14+
13. Scaling Various Components - Implement Horizontal Scaling.
15+
14. High-Performing Database Queries - Use Database Indexes.. 1point 3acres
16+
15. Bulk Job Processing - Consider Batch Processing & Message Queues.
17+
16. Server Load Management & Preventing DOS Attacks- Use a Rate Limiter.
18+
17. Microservices Architecture - Use an API Gateway.
19+
18. For Single Point of Failure - Implement Redundancy.
20+
19. For Fault-Tolerance and Durability - Implement Data Replication.
21+
20. For User-to-User fast communication - Use Websockets..--
22+
21. Failure Detection in Distributed Systems - Implement a Heartbeat.
23+
22. Data Integrity - Use Checksum Algorithm.
24+
23. Efficient Server Scaling - Implement Consistent Hashing.. ----
25+
24. Decentralized Data Transfer - Consider Gossip Protocol.
26+
25. Location-Based Functionality - Use Quadtree, Geohash, etc.
27+
26. Avoid Specific Technology Names - Use generic terms.
28+
27. High Availability and Consistency Trade-Off - Eventual Consistency.
29+
28. For IP resolution & Domain Name Query - Mention DNS (Domain Name System).. From 1point 3acres bbs
30+
29. Handling Large Data in Network Requests - Implement Pagination..1point3acres
31+
30. Cache Eviction Policy - Preferred is LRU (Least Recently Used) Cache.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def destroyTargets(self, nums: List[int], space: int) -> int:
3+
remainder_class = {} # remainder => (freq_counter, min_value)
4+
maxval = 0
5+
for num in nums:
6+
div, mod = divmod(num, space)
7+
if mod not in remainder_class:
8+
remainder_class[mod] = (1, num)
9+
else:
10+
remainder_class[mod] = (remainder_class[mod][0]+1, min(remainder_class[mod][1], num))
11+
12+
maxval = max(maxval, remainder_class[mod][0])
13+
14+
res = float('inf')
15+
for freq, minval in remainder_class.values():
16+
if freq == maxval:
17+
res = min(res, minval)
18+
return res

0 commit comments

Comments
 (0)