# Hit Counter - Amazon Top Interview Questions

### Problem Statement :

```Implement a hit counter which keeps track of number of the number of hits in the last 60 seconds.

count(int timestamp) which returns the number of hits that have been made in the last 60 seconds, given the current time is timestamp.
You can assume that the timestamps passed into add and count are monotonically increasing.

Constraints

n ≤ 100,000 where n is the number of calls that are made to add and count

Example 1

Input

arguments = [[], [10], [40], [40], [70], [100]]`

Output

[None, None, None, 2, None, 2]

Explanation

We create a HitCounter
We add timestamp 10 to the data structure
We add timestamp 40 to the data structure
We count the number of timestamps that are within last 60 seconds of 40. There's 10 and 40 so we
return 2
We add timestamp 70 to the data structure
We count the number of timestamps that are within last 60 seconds of 100. There's 40 and 70 so we return 2```

### Solution :

```                        ```Solution in C++ :

class HitCounter {
public:
vector<int> v;
HitCounter() {
}

v.push_back(timestamp);
}

int count(int timestamp) {
return v.end() - lower_bound(v.begin(), v.end(), timestamp - 60);
}
};```
```

```                        ```Solution in Python :

class HitCounter:
def __init__(self):
self.q = deque()

def _cleanup(self, timestamp):
while self.q and timestamp - self.q[0] > 60:
self.q.popleft()

self.q.append(timestamp)

def count(self, timestamp):
self._cleanup(timestamp)
return len(self.q)```
```

## Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary