Array Test (2 Hours)
This test covers the following topics:
- Two Pointers
- Precomputational Techniques
- Sorting (Bubble Sort, Selection Sort, Insertion Sort)
- ArrayList
- Hashing
Each question is designed to test your logical thinking and problem-solving skills. Detailed
examples are provided for better understanding.
Difficulty level: Medium
1. Maximum Water Container (Two Pointers)
Given an array `height[]` where `height[i]` represents the height of a vertical line at index `i`.
Find two lines that together with the x-axis form a container, such that the container holds
the maximum amount of water.
Input:
- n (2 ≤ n ≤ 10^5) — number of elements
- array[] (1 ≤ height[i] ≤ 10^4)
Output:
- Maximum amount of water that can be stored.
Examples:
Input:
6
186254
Output:
24
Input:
5
11111
Output:
4
Input:
8
3 9 3 4 7 2 12 5
Output:
36
2. Range Product Query (Prefix Product)
Given an array of integers and multiple queries, each query asks for the product of elements
between two given indices.
Input:
- n (1 ≤ n ≤ 10^5) — number of elements
- array[] (1 ≤ array[i] ≤ 10^9)
- q (1 ≤ q ≤ 10^5) — number of queries
- Each query contains two integers l and r (1 ≤ l ≤ r ≤ n)
Output:
- For each query, print the product of elements from index l to r.
Examples:
Input:
5
12345
3
13
24
15
Output:
6
24
120
Input:
4
10 2 5 3
2
12
24
Output:
20
30
Input:
3
789
1
13
Output:
504
3. Sort Colors (Dutch National Flag Algorithm)
Given an array containing 0s, 1s, and 2s, sort the array in place without using any built-in
sorting function.
Input:
- n (1 ≤ n ≤ 10^5) — number of elements
- array[] (each element is 0, 1, or 2)
Output:
- Sorted array
Examples:
Input:
5
20211
Output:
01122
Input:
6
221001
Output:
001122
Input:
3
021
Output:
012
4. Find the Leader Elements in an Array
An element is called a 'leader' if it is greater than or equal to all the elements to its right.
Input:
- n (1 ≤ n ≤ 10^5) — number of elements
- array[] (|array[i]| ≤ 10^9)
Output:
- List of leader elements in the order they appear in the array
Examples:
Input:
5
16 17 4 3 5
Output:
17 5
Input:
6
10 9 8 7 6 5
Output:
10 9 8 7 6 5
Input:
4
1234
Output:
4
5. Longest Consecutive Sequence (Hashing)
Given an unsorted array of integers, find the length of the longest consecutive sequence.
Input:
- n (1 ≤ n ≤ 10^5) — number of elements
- array[] (|array[i]| ≤ 10^9)
Output:
- Length of the longest consecutive sequence
Examples:
Input:
6
100 4 200 1 3 2
Output:
4
Input:
5
03725
Output:
1
Input:
7
1201345
Output:
6