GOOGLE | ONLINE ASSESSMENT |
Google has built several servers in a city. Each server can only service requests within its grid.
The grid of a server is formed by (0,0), (x, 0), (0, y), (x, y)
You are given the coordinates of servers in a list called SERVERS
Each list in the SERVERS list represents an
an x and y coordinate
SERVERS = [[x,y], [x,y],[x,y],...]
You are also given a list of coordinates called REQUESTS
REQUESTS = [[x,y], [x,y],[x,y],...]
Each request can be serviced if it lies within the rectangle or along the boundaries formed by the grid of a server
For each request in the REQUESTS list find out how many servers can service that request and return the answer in a list
EXAMPLE :
SERVERS = [[1,3], [4,1], [6,6]]
REQUEST = [[3,2], [1,1], [7,7]]
YOU SHOULD RETURN ---> ANSWER = [1, 3, 0]
EXPLANTION :
- WHY ANSWER[0] == 1 : ONLY THE SERVER AT [6, 6] CAN SERVICE THIS REQUEST
- WHY ANSWER[1] == 3 : ONLY THE SERVER AT [1, 3], [4, 1], [6, 6] CAN SERVICE THIS REQUEST
- WHY ANSWER[1] == 0 : NO SERVER CAN SERVICE THIS REQUEST
The obvious brute force solution is not too hard to come up with :
- Go through each request find out how many servers satisfy the propeprty
- x_server >= x_request && y_server >= y_request
What would be a better solution? I timed out on half the test cases. I could not figure out how to optimize it