Task Tracker
Programming Interview
Medium
8 views
Problem Description
You will receive q commands: ADD name, DONE name, COUNT. Create TaskTracker class. COUNT prints how many tasks are pending.
Input Format
First line q. Next q lines commands.
Output Format
Outputs for COUNT.
Sample Test Case
Input:
6
ADD study
ADD gym
COUNT
DONE study
COUNT
DONE gym
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
q=int(lines[0].strip())
class TaskTracker:
def __init__(self):
self.pending=set()
def add(self,name):
self.pending.add(name)
def done(self,name):
if name in self.pending:
self.pending.remove(name)
def count(self):
return len(self.pending)
t=TaskTracker()
out=[]
for i in range(1,1+q):
parts=(lines[i] if i<len(lines) else '').split()
if not parts: continue
if parts[0]=='ADD':
t.add(parts[1])
elif parts[0]=='DONE':
t.done(parts[1])
else:
out.append(str(t.count()))
print('\
'.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!