17 - Practice Questions
17 - Practice Questions
Question 1 :
Monotonic ArrayList(EASY)
An Arraylist is monotonic if it is either monotone increasing or monotone decreasing.
An Arraylist nums is monotone increasing if for all i <= j, nums.get(i) <= nums.get(j). An
Arraylist nums is monotone decreasing if for all i <= j, nums.get(i) >= nums.get(j).
Given an integer Arraylist nums, return true if the given list is monotonic, or false otherwise.
Constraints :
● 1 <= nums.size() <= 105
● -105 <= nums.get(i) <= 105
Question 2 :
Lonely Numbers in ArrayList(MEDIUM)
You are given an integer arraylist nums. A number x is lonely when it appears only once, and
no adjacent numbers (i.e. x + 1 and x - 1) appear in the arraylist.
Return all lonely numbers in nums. You may return the answer in any order.
Constraints :
● 1 <= nums.size() <= 105
● 0 <= nums.get(i) <= 106
Question 3 :
Most Frequent Number following Key(EASY)
You are given an integer Arraylist nums. You are also given an integer key, which is present in
nums.
For every unique integer target in nums, count the number of times target immediately follows
an occurrence of key in nums. In other words, count the number of indices i such that:
Constraints :
● 2 <= nums.size() <= 1000
● 1 <= nums.get(i) <= 1000
● Assume that the answer is unique.
Hints: Count the number of times each target valuefollows the key in the arraylist.
Choose the target with the maximum count and return it.
Question 4 :
Beautiful ArrayList(MEDIUM)
An Arraylist nums of size n is beautiful if:
Sample Input 1: n = 4
Sample Output 1: [2,1,4,3]
Sample Input 2: n = 5
Sample Output 2: [3,1,2,5,4]
Constraints :
● 1 <= n <= 1000