Login

Top Apple Coding Interview Questions

Problem title

Difficulty

Avg time to solve

Maximum Size Rectangle Sub-matrix With All 1's

Hard

10 mins

Minimum Number Of Taps To Water Garden

Hard

15 mins

Morse Code To English

Easy

15 mins

Print Nodes at Distance K From a Given Node

Hard

20 mins

Spiral Matrix

Easy

15 mins

Inplace rotate matrix 90 degree

Easy

12 mins

House Robber

Moderate

15 mins

Merge Two Sorted Linked Lists

Moderate

15 mins

Palindrome Partitioning

Hard

25 mins

Right View

Moderate

35 mins

Problem

Submissions

7

Avg. time to solve

15 min

Success Rate

85%

Problem Statement

```
Can you solve the problem in O(N) time?
```

```
The first line contains a single integer T representing the number of test cases.
The first line of each test case will contain the integer N.
The second and the last line of each test case will contain N single space-separated integers representing the elements of the array “ranges”.
```

```
For each test case, print a single integer representing the value of the minimum number of taps needed to open by the gardener to fill the whole garden.
```

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 10
1 <= N <= 10^4
0 <= ranges[i] <= 100
Time Limit: 1 sec
```

```
2
3
0 0 0 0
7
1 2 1 0 2 1 0 1
```

```
-1
3
```

```
In test case 1, the ranges of taps are as follows : [ [ 0, 0 ], [ 1, 1 ], [ 2, 2 ] ]. So in the worst case, if we open all the taps, then it’s impossible to fill the gaps i.e (0-1), (1,2), (2,3). So it’s impossible to fill the garden.
In test case 2, the ranges of taps are as follows : [ [ -1, 1 ],[ -1, 3 ],[ 1, 3 ],[ 3, 3 ],[ 2, 6 ],[ 4, 6 ],[ 6, 6 ],[ 6, 8 ] ]. To fill the garden i.e [ 0, 7 ] , the gardener needs to open a minimum of three taps i.e. tap 2: [ -1, 3 ] , tap 5: [ 2, 6 ], tap 8: [ 6, 8 ] to fill the whole garden.
```

```
2
8
4 0 0 0 0 0 0 0 4
8
4 0 0 0 4 0 0 0 4
```

```
2
1
```

Console

Sample Test Case

Custom Test Case

Download Test Cases

Test Case 1

Test Case 2

Test Case 3

Saving Code...

Full Screen Mode

Change Language

Change Theme

Solution submission not allowed

Save Code

Reset Code