Hot Topics

Navi Solution
Technical Round
import threading
class EventQueue:
def __init__(self):
self.subscribers = []
self.queue = []
self.lock = threading.Lock()
def subscribe(self, subscriber):
with self.lock:
self.subscribers.append(subscriber)
def unsubscribe(self, subscriber):
with self.lock:
self.subscribers.remove(subscriber)
def enqueue(self, event):
with self.lock:
self.queue.append(event)
self._notify()
def _notify(self):
for subscriber in self.subscribers:
threading.Thread(target=subscriber.notify, args=(self.queue,)).start()
def dequeue(self):
with self.lock:
return self.queue.pop(0)
class Subscriber:
def __init__(self, name):
self.name = name
def notify(self, queue):
event = queue.dequeue()
print(f'{self.name} received event: {event}')
In this example, the EventQueue
class is the main component of the system. It maintains a list of subscribers, a queue of events, and a lock to control access to the shared data. The subscribe
method is used to add a new subscriber to the list, the unsubscribe
method is used to remove a subscriber from the list, and the enqueue
method is used to add a new event to the queue. The _notify
method is used to notify all the subscribers that there is a new event in the queue, it does this by starting a new thread for each subscriber, this way the subscribers will be notified concurrently.
The Subscriber
class represents a consumer of events. It has a name
attribute and a notify
method that is called when there is a new event in the queue. The notify
method removes the next event from the queue and prints the event.
In order to use this event-based queue, you would first create an instance of the EventQueue
class, then create instances of the Subscriber
class and subscribe them to the event queue, and then enqueue events to the queue.
queue = EventQueue()
subscriber1 = Subscriber("Subscriber 1")
subscriber2 = Subscriber("Subscriber 2")
queue.subscribe(subscriber1)
queue
def find_paths(root, sum, path, paths):
if not root:
return
path.append(root.val)
if root.val == sum and not root.left and not root.right:
paths.append(list(path))
else:
find_paths(root.left, sum-root.val, path, paths)
find_paths(root.right, sum-root.val, path, paths)
path.pop()
def find_paths_with_sum(root, sum):
paths = []
find_paths(root, sum, [], paths)
return paths
This function will find all the path from root to leaf in the tree, that sum up to k.
Keep in mind that this approach has a time complexity of O(N^2) where N is the number of nodes in the tree, as it’s traversing all the nodes and for each node, it’s traversing the path and checking if the sum matches k.
def find_largest_substring(s):
left = 0
middle = 0
right = 0
max_length = 0
char_map = {}
while right < len(s):
if s[right] not in char_map:
char_map[s[right]] = right
right += 1
max_length = max(max_length, right - left)
elif middle <= char_map[s[right]]:
left = char_map[s[right]] + 1
middle = left
char_map[s[right]] = right
right += 1
else:
middle += 1
char_map[s[right]] = right
right += 1
return max_length
This approach uses a sliding window, and with the help of a hashmap to keep track of the last position of each character, it’s able to determine if a character is repeating or not. The time complexity of this approach is O(n) and the space complexity is O(n) as well.




Popular Category
Hot Topics
Go through our study material. Your Job is awaiting.