Skip to content

Commit b6993f0

Browse files
author
Partho Biswas
committed
Google Phone Interview Mock 1
1 parent b9edce1 commit b6993f0

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ I have solved quite a number of problems from several topics. See the below tabl
166166
|55| **[683. K Empty Slots](https://tinyurl.com/rbyxknc)** | [Python](https://tinyurl.com/wu6rdaw/683_K_Empty_Slots.py), [Swift](https://tinyurl.com/wuja3c4/683_K_Empty_Slots.swift) | [Art 1](https://tinyurl.com/qsenvnd), [Art 2](https://tinyurl.com/u7g6hhy), [Art 3](https://tinyurl.com/vw42pp7) | Hard | TODO: Check monotonic queue approach. Solved it with sliding widow |
167167
|56| **[849. Maximize Distance to Closest Person](https://tinyurl.com/v44bs88)** | [Python](https://tinyurl.com/wu6rdaw/849_Maximize_Distance_to_Closest_Person.py), [Swift](https://tinyurl.com/wuja3c4/849_Maximize_Distance_to_Closest_Person.swift) | [Art 1](https://tinyurl.com/qsenvnd) | Easy | Not so easy and intuitive. Can be solve is a variety of way. I have used Binary search(A bit over engineered solution). Can be simplifies |
168168
|57| **[283. Move Zeroes](https://tinyurl.com/y5kxjuvc)** | [Python](https://tinyurl.com/wu6rdaw/283_Move_Zeroes.py), [Swift](https://tinyurl.com/wuja3c4/283_Move_Zeroes.swift) | --- | Easy | Not so easy and intuitive. Uses fast and slow pointer |
169+
|58| **[163. Missing Ranges](https://tinyurl.com/s297jm7)** | [Python](https://tinyurl.com/wu6rdaw/163_Missing_Ranges.py), [Swift](https://tinyurl.com/wuja3c4/163_Missing_Ranges.swift) | --- | Medium | Man, it's a very trick problem |
169170

170171

171172
</p>
@@ -212,6 +213,7 @@ I have solved quite a number of problems from several topics. See the below tabl
212213
|31| **[949. Largest Time for Given Digits](https://tinyurl.com/ux8dk9e)** | [Python](https://tinyurl.com/wu6rdaw/949_Largest_Time_for_Given_Digits.py), [Swift](https://tinyurl.com/wuja3c4/949_Largest_Time_for_Given_Digits.swift) | --- | Easy | Not so easy |
213214
|32| [788. Rotated Digits](https://tinyurl.com/t9gelfm) | [Python](https://tinyurl.com/wu6rdaw/788_Rotated_Digits.py), [Swift](https://tinyurl.com/wuja3c4/788_Rotated_Digits.swift) | --- | Easy | --- |
214215
|33| [388. Longest Absolute File Path](https://tinyurl.com/svfa3rc) | [Python](https://tinyurl.com/wu6rdaw/388_Longest_Absolute_File_Path.py), [Swift](https://tinyurl.com/wuja3c4/388_Longest_Absolute_File_Path.swift) | --- | Medium | --- |
216+
|34| [616. Add Bold Tag in String](https://tinyurl.com/taftav2) | [Python](https://tinyurl.com/wu6rdaw/616_Add_Bold_Tag_in_String.py), [Swift](https://tinyurl.com/wuja3c4/616_Add_Bold_Tag_in_String.swift) | --- | Medium | Merge intervals(hidden) |
215217

216218

217219
</p>
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def findMissingRanges(self, nums, lower, upper):
3+
"""
4+
:type nums: List[int]
5+
:type lower: int
6+
:type upper: int
7+
:rtype: List[str]
8+
"""
9+
nums.insert(0, lower - 1)
10+
nums.append(upper + 1)
11+
res = []
12+
i = 0
13+
while i < len(nums) - 1:
14+
leftRage, rightRange = nums[i], nums[i + 1]
15+
if leftRage != (rightRange - 1):
16+
if rightRange - leftRage == 2:
17+
res.append(str(rightRange - 1))
18+
elif rightRange - leftRage > 2:
19+
res.append("{}->{}".format(leftRage + 1, rightRange - 1))
20+
i += 1
21+
return res

leetcode.com/python/346_Moving_Average_from_Data_Stream.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
from collections import deque
22

3-
43
class MovingAverage(object):
54

6-
def __init__(self, size: int):
5+
def __init__(self, size):
76
"""
87
Initialize your data structure here.
98
:type size: int
109
"""
1110
self.size = size
12-
self.queue = deque()
13-
self.widowSum = 0
11+
self.nums = deque()
1412

15-
def next(self, val: int) -> float:
13+
def next(self, val):
1614
"""
1715
:type val: int
1816
:rtype: float
1917
"""
20-
self.queue.append(val)
21-
tail = self.queue.popleft() if len(self.queue) > self.size else 0
22-
self.widowSum = self.widowSum - tail + val
23-
res = self.widowSum / min(len(self.queue), self.size)
24-
return res
18+
if len(self.nums) >= self.size:
19+
self.nums.popleft()
20+
self.nums.append(val)
21+
return float(float(sum(self.nums)) / float(len(self.nums)))
2522

2623
# Your MovingAverage object will be instantiated and called as such:
2724
# obj = MovingAverage(size)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Approach 1: Merge intervals
2+
# Step 1: create a list of tuples/intervals with opening/closing positions, e.g. (open_index, close_index)
3+
# Step 2: merge the list of intervals (see https://leetcode.com/problems/merge-intervals/)
4+
# Step 3: go through the merged interval list and insert the tags into the string
5+
class Solution(object):
6+
def addBoldTag(self, s, dict):
7+
"""
8+
:type s: str
9+
:type dict: List[str]
10+
:rtype: str
11+
"""
12+
locations = []
13+
# generate the word intervals
14+
for word in dict:
15+
startIdx = s.find(word)
16+
while startIdx != -1: # "s" can contain same word multiple times ad overlapped
17+
locations.append([startIdx, startIdx + len(word) - 1])
18+
startIdx = s.find(word, startIdx + 1)
19+
20+
if not locations:
21+
return s
22+
23+
# merge the intervals
24+
locations.sort(key=lambda x: x[0])
25+
locationsMerged = []
26+
startIdx, endIdx = locations[0][0], locations[0][1]
27+
for i in range(1, len(locations)):
28+
nextStart, nextEnd = locations[i][0], locations[i][1]
29+
if nextStart <= endIdx + 1: # since, if the strings are consequtive
30+
endIdx = max(endIdx, nextEnd)
31+
else:
32+
locationsMerged.append([startIdx, endIdx])
33+
startIdx, endIdx = nextStart, nextEnd
34+
locationsMerged.append([startIdx, endIdx])
35+
36+
# gennerate the old tagged string
37+
resultStr = ""
38+
startIdx, endIdx = locationsMerged[0][0], locationsMerged[0][1]
39+
resultStr += s[0:startIdx]
40+
for i in range(len(locationsMerged)):
41+
nextStart, nextEnd = locationsMerged[i][0], locationsMerged[i][1]
42+
if endIdx < nextStart:
43+
resultStr += s[endIdx + 1:nextStart]
44+
strToBold = s[nextStart:nextEnd + 1]
45+
if strToBold:
46+
resultStr += "<b>{}</b>".format(strToBold)
47+
else:
48+
resultStr += s[endIdx + 1:nextStart]
49+
startIdx, endIdx = nextStart, nextEnd
50+
resultStr += s[endIdx + 1:]
51+
return resultStr

0 commit comments

Comments
 (0)