Amazon OA | April 2022 | Served Buildings & Number of ways to choose 3 pages
Anonymous User
5341

There were 2 questions and I had to write brief explanation of my solutions with time and space complexity. The test was on HackerRank.

1) Find the number of served buildings

Given:

  • Head count for a list of buildings
  • Array of range for each router
  • Array of location of each router

If a router location is i and it's range is k then it will serve buildings at indices i-k to i+k inclusive.

A building is considered as served if the number of routers serving that building is greater than or equal to head count of that building.

Test case 1:
headCount: [2, 3, 3, 1, 5, 6]
routerLocation: [2, 4, 1]
routerRange: [2, 4, 3]

Number of routrers serving each building would be [2, 2, 3, 3, 3, 1] so buildings at indices. 0, 2 and 3 would be considered as served and hence the answer would be 3 (number of served buildings).

2) Number of ways to choose 3 pages

Given:

  • A binary string that represents pages of a book (0011010). 1 represents a bookmarked page and 0 represents non-bookmarked flag.

Find the number of ways to choose 3 pages (i, j, k) such that i < j < k and consecutive pages in the selection is not same.
Valid selecitons: "010" and "101"
Invalid selections: All other selections (110, 000, 111, etc.)

Test Case 1:
book: "00101"

There are total 3 ways to choose pages which are "010", "010" and "101". So, the answer is 3.

Comments (10)