Programming Interview QuestionsOnly knowing the theoretical aspects of Java is not sufficient to crack the interview. One of the most important rounds for becoming a Java developer is the programming round, where the interviewee is asked to code. Writing optimized code is a must for becoming a Java developer. In this tutorial, we will discuss some advanced programming interview questions. 1) Given an integer array arr of size s. Now, check whether there exists at least one element which is strictly smaller than all the elements on the right of it and greater than all the elements on the left of it. If it exists, return true; otherwise, return false. Ignore the corner elements of the given array.Example: 1 arr[] = {3, 4, 5, 1, 6, 10, 9, 7, 8}; s = 9 Ans: true Explanation: All the numbers that are coming before 6 are smaller than 6, and all the elements that are coming after 6 are greater than 6. Hence, the answer is true. Example: 2 arr[] = {4, 5, 1}; s = 3 Ans: false Explanation: There is not a single number that fits the given condition. Hence, the answer is false. FileName: PeakElement.java Output: The array [10, 2, 3, 4, 5, 4, 3, 2, 3, 10] has not got a peak element. The array [3, 4, 5, 1, 6, 10, 9, 7, 8] has got a peak element. 2) Given a read-only array arr of size s, such that each element of the array can not exceed the value s. Also, in the array, each element occurs only once except a1, which is occurring twice, and therefore, a2 is missing. Find out the value of a1 and a2. Note that read-only means any modification in the input array is not allowed.Example: 1 arr[] = {1, 3, 5, 2, 3}; s = 5 Ans: a1 = 3, a2 = 4 Explanation: The repeating number is 3, and the missing number is 4. Example: 2 arr[] = {1, 2, 1}; s = 3 Ans: a1 = 1, a2 = 3 Explanation: The repeating number is 1, and the missing number is 3. FileName: MissingAndRepeatingElement.java Output: For array: [1, 3, 5, 4, 1] a1 is: 1 a2 is: 2 For array: [1, 3, 5, 2, 3] a1 is: 3 a2 is: 4 3) Given a read-only array arr of size s. Find out the number of ways one can divide the array arr into 3 parts such that the sum of each part is equal.Example: 1 arr[] = {0, 1, -1, 0}; s = 4 Ans: 1 Explanation: There is only one way to split the array into three parts whose sum is equal. {0}, {1, -1}, {0} Example: 2 arr[] = {2, 2, 4, 0, 4}; s = 5 Ans: 2 Explanation: There are two ways to split the array into three parts whose sum is equal. {2, 2}, {4, 0}, {4} {2, 2}, {4}, {0, 4} FileName: ParititionArray.java Output: For array: [2, 2, 4, 0, 4] The number of ways to it partition is: 2 For array: [-1, 1, 0, 0, -1, 1] The number of ways to it partition is: 3 For array: [4, 6, 8, 9, -13, 9] The number of ways to it partition is: 0 4) An array arr of N integers X1, X2, X3, ...., Xn is given. You have to return the maximum value of f(a1, a2), where 0 ≤ a1, a2, < N. It is given that f(a1, a2) = |arr[a1] - a[a2]| + |a1 - a2|, where |y| represents the absolute value of y.Example: 1 arr[] = {1, -1, 3}; Ans: 5 Explanation: f(0, 0) = f(1, 1) = f(2, 2) = 0 f(0, 1) = f(1, 0) = 3 f(0, 2) = f(2, 0) = 4 f(1, 2) = f(2, 1) = 5 We see that the maximum value is 5. Hence, the answer is 5. Example: 2 arr[] = {2, -1, -1, 3}; Ans: 6 Explanation: f(0, 0) = f(1, 1) = f(2, 2) = f(3, 3) = 0 f(0, 1) = f(1, 0) = 4 f(0, 2) = f(2, 0) = 5 f(0, 3) = f(3, 0) = 4 f(1, 2) = f(2, 1) = 1 f(1, 3) = f(3, 1) = 6 f(2, 3) = f(3, 2) = 5 We see that the maximum value is 6. Hence, the answer is 6. FileName: AbsoluteValue.java Output: The maximum absolute value of the array: [1, 3, -1] is 5 The maximum absolute value of the array: [3, 4, 5, 9, 12] is 13 The maximum absolute value of the array: [13, -4, 5, -12] is 28 The maximum absolute value of the array: [-1, 2, 9, -7] is 17 5) Find the next permutation of the number that is formed by the elements of the array arr of size s without changing the order of elements.Example: 1 arr[] = {3, 1, 2}; s = 3; Ans: 321 Explanation: The number formed using the array elements is 312. The next permutation of the number 312 is 321. Example: 2 arr[] = {1, 2, 3}; Ans: 132 Explanation: The number formed using the array elements is 123. The next permutation of the number 123 is 132. FileName: NextPermutation.java Output: The next permutation of 123 is 132 The next permutation of 1432 is 2134 The next permutation of 13794 is 13947 6) There is an array arr, which contains only non-negative numbers. Create the largest number using all of the non-negative numbers of the given array arr.Example: 1 arr[] = {35, 1, 9}; Ans: 9351 Explanation: 9 is the largest number digit, so it will be put first. After that, the digit 3 is greater than digit 1. Therefore, 35 is put before 1. Example: 2 arr[] = {17, 22, 83, 7, 5}; Ans: 83752217 Explanation: 8 is the largest digit in the given array. Therefore, 83 is put first. The same treatment is given to other numbers too. FileName: LargestNoExample.java Output: The largest number formed from the array: [17, 22, 83, 7, 5] is 83752217 The largest number formed from the array: [79, 21, 83, 7, 58] is 837975821 The largest number formed from the array: [0, 9, 99, 79, 87, 158] is 99987791580 7) A 2-dimensional matrix containing m rows and m columns is given. The task is to do the rotation of the matrix by 90 degrees in a clockwise manner.Example: 1 Example: 2 FileName: RotateMatrix.java Output: The array is: 1 3 2 4 After rotating to 90 degrees clockwise the array is: 2 1 4 3 The array is: 4 8 7 2 9 5 6 3 0 8 1 6 4 3 2 5 After rotating to 90 degrees clockwise the array is: 4 0 9 4 3 8 5 8 2 1 6 7 5 6 3 2 8) There is an array arr, which contains only non-negative numbers. Find the subarray of the smallest size from the given array arr, which gets sorted; then the whole array gets sorted.Example: 1 arr[] = {2, 4, 3, 8, 9}; Ans: {1, 2} Explanation: The subarray starting from index 1 and ending at index 2 is the smallest subarray that gets sorted, then the whole array gets sorted. Example: 2 arr[] = {12, 34, 45, 67, 78, 88, 99, 102}; Ans: { } Explanation: The given array is already sorted. Hence, the subarray is empty. FileName: SmallestSubArray.java Output: The array is: [2, 4, 3, 8, 9] Minimum subarray for sorting the array is: [4, 3] The array is: [12, 34, 45, 67, 78, 88, 99, 102, 345, 555, 666, 777, 888, 999] Minimum subarray is not found as the array is already sorted The array is: [1, 2, 3, 3, 4, 4, 5, 5, 7, 8, 9, 20, 14, 16, 19, 14, 19, 19, 20, 21, 22, 27, 30] Minimum subarray for sorting the array is: [20, 14, 16, 19, 14, 19, 19] 9) An array arr with size sz, a key k, and the segment size s is given, such that s completely divides sz. Check whether each segment of the array arr of size s has the key k present or not.Example: 1 arr[] = {5, 3, 2, 4, 3, 9, 11, 3, 12, 67, 45, 3}; sz = 12; k = 3; s = 3; Ans: Yes, each segment of the array arr of size s contains the key k. Explanation: sz / s = 12 / 3 = 4. These four segments are: {5, 3, 2} -----> contains the key 3 {4, 3, 9} -----> contains the key 3 {11, 3, 12} -----> contains the key 3 {67, 45, 3} -----> contains the key 3 Thus, each segment contains the key k, which is 3. Example: 2 arr[] = {23, 56, 65, 21, 34, 67, 89, 9, 0, 23, 55, 44, 33, 22, 23}; sz = 15; k = 23; s = 5; Ans: Yes, each segment of the array arr of size s contains the key k. Explanation: sz / s = 15 / 5 = 3. These three segments are: {23, 56, 65, 21, 34} -----> contains the key 23 {67, 89, 9, 0, 23} -----> contains the key 23 {55, 44, 33, 22, 23} -----> contains the key 23 Thus, each segment contains the key k, which is 23. FileName: ArraySegmentKey.java Output: For array [5, 3, 2, 4, 3, 9, 11, 3, 12, 67, 45, 3] Each segment of size 3 contains the key 3 For array [23, 56, 65, 21, 34, 67, 89, 9, 0, 23, 55, 44, 33, 22, 23] Each segment of size 5 contains the key 23 For array [1, 6, 7, 2, 3, 8, 9] Each segment of the array of size 1 does not contain the key 6 10) Given a string, the task is to search the rank of the given string amongst its lexicographically sorted permutations. The characters of the given string may or may not be repeated. If the characters are repeated, one needs to look at the unique permutations and then determine the rank.Example: 1 str = "caa" Ans: The rank of "caa" is 3 among its lexicographically sorted permutations. Explanation: The string has three characters, 'a', 'a', and 'c', and their lexicographically sorted permutations are: "aac" ----------> rank - 1 "aca" ----------> rank - 2 "caa" ----------> rank - 3 Hence, the rank is 3. Example: 2 str = "dcab" Ans: The rank of "dcab" is 23 among its lexicographically sorted permutations. Explanation: The string has four characters, 'a', 'b', 'c', 'd', and their lexicographically sorted permutations are: "abcd" ----------> rank - 1 "abdc" ----------> rank - 2 "acbd" ----------> rank - 3 "acdb" ----------> rank - 4 "adbc" ----------> rank - 5 "adcb" ----------> rank - 6 "bacd" ----------> rank - 7 "badc" ----------> rank - 8 "bcad" ----------> rank - 9 "bcda" ----------> rank - 10 "bdac" ----------> rank - 11 "bdca" ----------> rank - 12 "cabd" ----------> rank - 13 "cadb" ----------> rank - 14 "cbad" ----------> rank - 15 "cbda" ----------> rank - 16 "cdab" ----------> rank - 17 "cdba" ----------> rank - 18 "dabc" ----------> rank - 19 "dacb" ----------> rank - 20 "dbac" ----------> rank - 21 "dbca" ----------> rank - 22 "dcab" ----------> rank - 23 Hence, the rank is 23. FileName: PermutationRank.java Output: The rank of the string 'caa' is 3 among its lexicographically sorted permutations. The rank of the string 'dcab' is 23 among its lexicographically sorted permutations. The rank of the string 'haaxxy' is 61 among its lexicographically sorted permutations. The rank of the string 'mttlks' is 177 among its lexicographically sorted permutations. 11) Given two sorted arrays, arr and arr1. Their size may not be or maybe equal. Find the median of these given arrays. Note that merging of the sorted arrays is not allowed.Example: 1 arr = {-11, -10, -9, -2, 5, 17} arr1 = {-4, 4, 7, 13, 16, 17, 19} Ans: The median of these sorted arrays is at the index. Explanation: When these sorted arrays are merged, we get the following. mergedArray = {-11, -10, -9, -4, -2, 4, 5, 7, 13, 16, 17, 17, 19}; The size of the mergedArray is 13, which is odd. Hence, there is only one middle element sitting at the index 13 / 2 = 6. At the 6^{th} index, we get 5. Hence, 5 is the median of these sorted arrays. Note: We are merging it here only for the explanation purpose. In the code, we will not merge the arrays.Example: 2 arr = {5, 7, 9, 11, 13, 16, 19, 22} arr1 = {-4, 4, 7, 8, 10, 11, 12, 14} Ans: The median of these sorted arrays is 10. Explanation: When these sorted arrays are merged, we get the following. mergedArray = {-4, 4, 5, 7, 7, 8, 9, 10, 11, 11, 12, 13, 14, 16, 19, 22}; The size of the mergedArray is 16, which is even. Hence, there are two middle elements which are sitting at 16 / 2 = 8^{th} and 16 / 2 - 1 = 7^{th} indices. Thus, the median is (10 + 11) / 2 = 10.5. FileName: FindMedian.java Output: The median element is: 5.0 The median element is: 10.5 12) Given an array arr whose size is s. Also, an integer tar is given. The array arr is rotated using an element of the array as pivot {1, 2, 3, 4, 5} might become {4, 5, 1, 2, 3}. Find whether the integer tar is present in the array arr or not. Note that the array arr is sorted in non-decreasing order before the rotation.Example: 1 arr = {7, 8, 9, 10, 0, 3, 4, 5, 6}; s = 9; tar = 3; Ans: 5. Explanation: the number 3 is present at the index 5. Example: 2 arr = {4, 5, 1, 2, 3}; s = 5; tar = 6; Ans: -1 Explanation: the number 6 is not present in the array. FileName: FindElement.java Output: The target element 3 is found at the index 5 The target element is not found! Note: The linear search will also do the job here. However, its time complexity would be more as compared to the binary search.13) Given an array arr whose size is s. The array arr is sorted in non-decreasing order. Also, an integer tar is given. Find whether the integer tar is present in the array arr or not. If present, find the starting and ending indices of the integer tar.Example: 1 arr = {1, 2, 3, 4, 5, 5, 5, 5, 6, 7, 8, 9, 10}; s = 13; tar = 5; Ans: starting index: 4, ending index: 7 Explanation: Between the indices 4 to 7, the number 5 is present. Example: 2 arr = {8, 10, 12, 56, 67, 70, 70, 70, 70, 75, 77, 78, 90, 93, 97}; s = 15; tar = 19; Ans: The target number 19 is not present in the given array FileName: FindRange.java Output: Range of target element 5 is: [4, 7] The target element is not found! 14) A string str is given. Find the minimum of characters added to the string str to make it a palindrome string. The characters must be added at the beginning of the string.Example: 1 str = "ABC"; Ans: 2 Explanation: Inserting "B" at the beginning, we get B + ABC ---> BABC. Inserting "C" at the beginning, we get C + BABC ---> CBABC. Thus, there are 2 insert operations. Hence, the answer is 2. Example: 2 str = "ABEBAAAA"; Ans: 3 Explanation: Inserting "A" at the beginning, we get A + ABEBAAAA ---> AABEBAAAA. Inserting "A" at the beginning, we get A + AABEBAAAA ---> AAABEBAAAA. Inserting "A" at the beginning, we get A + AAABEBAAAA ---> AAAABEBAAAA. Thus, there are 3 insert operations. Hence, the answer is 3. FileName: MakePalindrome.java Output: 2 insert operations are required to make 'ABC' a palindrome string. 3 insert operations are required to make 'ABEBAAAA' a palindrome string. 15) An array of strings and a number n is given. Format the strings present in the array in such a way that each line contains n characters. Insert the strings using the greedy approach, which means insert as many words or characters as one can in a line. Use ' ' character for the padding purpose so that each line has n characters. The extra spaces should be evenly distributed. If the number of spaces does not divide the strings evenly, then the left slot should have more spaces as compared to the right ones. For the last line, the text should be left-justified, i.e., extra spaces should be more on the right side.Example: Words = {"It", "is", "the", "example", "of", "sorting", "technique"} N = 16 Ans: the formatted lines are: FileName: TextJustified.java Output: 'It is the' 'example of' 'sorting ' 'technique ' 16) An integer array arr is given such that every number is repeated thrice, barring one, which is repeated only once. Return the number that is repeated only once.Example: arr[] ={2, 2, 2, 4, 6, 6, 6, 5, 4, 9, 9, 9, 0, 0, 4, 0} Ans: 5 Explanation: Number 5 is only repeated once. FileName: UniqueNumber.java Output: Array is: [2, 2, 2, 4, 6, 6, 6, 5, 4, 9, 9, 9, 0, 0, 4, 0] The unique number is: 5 Array is: [6, 6, 6, 1, 1, 1, 4, 4, 4, 12, 0, 0, 0] The unique number is: 12 17) An integer array arr is given. Rearrange array arr such that arr[i] = arr[arr[i]]. The space complexity must be O(1).Example: arr[] = {2, 1, 0} Ans: {0, 1, 2}; Explanation: arr[0] = 2 => arr[arr[0]] becomes arr[2] = 0; arr[1] = 1 => arr[arr[1]] = arr[1] = 1 arr[2] = 0 => arr[arr[2]] = arr[0] = 2 Thus, the updated array is {0, 1, 2} FileName: RearrangeArray.java Output: Array before rearranging is: [2, 1, 0] Array after rearranging is: [0, 1, 2] Array before rearranging is: [2, 4, 3, 1, 0] Array after rearranging is: [3, 0, 1, 4, 2] Note: if the O(1) space complexity condition is removed, then the problem is straightforward. One can create an array of the same size as of input array and can copy the values of a[a[i]] to the recently created array. The O(1) space complexity makes the problem tricky.18) A positive number n is given. Find the number of trailing zeros that are present in the binary representation of the number.Example: 1 n = 20 Ans: 2 Explanation: The binary representation of 20 is 10100. The total number of trailing zeros present in 10100 is 2. Hence, the answer is 2. Example: 2 n = 9 Ans: 0 Explanation: The binary representation of 9 is 1001. The total number of trailing zeros present in 1001 is 0. Hence, the answer is 0. FileName: TrailingZeros.java Output: For number 20, there are 2 trailing zeros. For number 64, there are 6 trailing zeros. For number 3, there are 0 trailing zeros. 19) A number n is given. Find the n^{th} number whose binary representation is a palindrome.Example: 1 n = 1 Ans: 1 Explanation: The binary representation of 1 is 1, and 1 is a palindrome. 1 is also the 1^{st} number. Example: 2 n = 3 Ans: 5 Explanation: The 3^{rd} number whose binary representation is a palindrome is 5. Its binary representation is 101. Example: 3 n = 9 Ans: 27 Explanation: The 9^{th} number whose binary representation is a palindrome is 27. Its binary representation is 11011. FileName: PalindromeNumbers.java Output: The 7th number whose binary representation is palindrome is: 17 The 13th number whose binary representation is palindrome is: 51 20) An array arr of integers is given. Also, a sliding window of size w is moving from the left side to the right side of the array. Thus, at a time, one can only watch w numbers. The task is to find the maximum for each window. Observe the following examples.Example: arr[] = {1, 4, -1, -4, 6, 4, 7, 8}; w = 3; Ans: {4, 4, 6, 6, 7, 8} Explanation: {[1, 4, -1,] -4, 6, 4, 7, 8} -> max(1, 4, -1) = 4 {1, [4, -1, -4,] 6, 4, 7, 8} -> max(4, -1, -4) = 4 {1, 4, [-1, -4, 6,] 4, 7, 8} -> max(-1, -4, 6) = 6 {1, 4, -1, [-4, 6, 4,] 7, 8} -> max(-4, 6, 4) = 6 {1, 4, -1, -4, [6, 4, 7,] 8} -> max(6, 4, 7) = 7 {1, 4, -1, -4, 6, [4, 7, 8]} -> max(4, 7, 8) = 8 FileName: SlidingWindowMax.java Output: The array is: [1, 4, -1, -4, 6, 4, 7, 8] The maximum values are: [4, 4, 6, 6, 7, 8] 21) An arithmetic expression is given in Reverse Polish Notation. Evaluate the given expression.Example: A = ["4", "3", "+", "5", "*"] Ans: 35 Explanation: * -> () * () 5: -> () * (5) +: (() + ()) * (5) 3: (() + (3)) * (5) 4: ((4) + (3)) * (5) = (7) * (5) = 35 FileName: EvaluateExpression.java Output: The expression is: [4, 3, +, 5, *] The value of the expression is: 35 The expression is: [4, 13, 5, /, +] The value of the expression is: 6 22) A number n is given, which represents the total number of bits present in the code. The gray code always starts from 0. Display the sequence of the gray code.Example: N = 3; Ans: {0, 1, 3, 2, 6, 7, 5, 4} 0 0 0 ---> 0 0 0 1 ---> 1 0 1 1 ---> 3 0 1 0 ---> 2 1 1 0 ---> 6 1 1 1 ---> 7 1 0 1 ---> 5 1 0 0 ---> 4 FileName: GrayCode.java Output: For n = 3, the gray code sequence is: [0, 1, 3, 2, 6, 7, 5, 4] For n = 4, the gray code sequence is: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] For n = 5, the gray code sequence is: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16] 23) An array arr containing n integers is given. Find all the unique triplets (x, y, and z) such that x + y + z = 0. Note that ordering of triplets does not matter, i.e., y + z + x or y + x + z is the same as x + y + z.Example: arr = {-2, 0, 2, 4, -2, -4}; Ans: There are three unique triplets. {-2, 0, 2}, {-2, -2, 4}, {-4, 0, 4} FileName: FindTriplets.java Output: For the list: [-4, -2, -2, 0, 2, 4] Triplets are: [[-4, 0, 4], [-2, -2, 4], [-2, 0, 2]] For the list: [-5, -4, -4, -4, -3, -2, -1, -1, -1, 0, 0, 0, 1, 1, 1, 3, 4, 4, 5] Triplets are: [[-5, 0, 5], [-5, 1, 4], [-4, -1, 5], [-4, 0, 4], [-4, 1, 3], [-3, -2, 5], [-3, -1, 4], [-3, 0, 3], [-2, -1, 3], [-2, 1, 1], [-1, 0, 1], [0, 0, 0]] 24) There is an array arr, which contains only non-negative integers (x1, x2, x3, ..., xn). Each of which represents the coordinate (j, xj). 'n' vertical lines are stretched such that the two endpoints of the line j is at (j, xj) and (j, 0). Find such two lines, which simultaneously with the x-axis creates a container, such that the container fetches the maximum amount of water. Assume that the water can be stored in the 2-D container. Note that slanting the container is not allowed.Example: 1 arr = {5, 1, 2, 3}; Ans: 9 Explanation: There are 4 vertical lines in the given array, which are of height 5, 1, 2, and 3, respectively. Also, 5 and 3 are 3 distances apart. So, the capacity is min(5, 3) * 3 = 9. If we take any other pair of the vertical lines, we do not get a capacity of more than 9. FileName: ContainerCapacity.java Output: For the list: [5, 1, 2, 3] The maximum capacity is: 9 25) An array arr of n integers is given. x1, x2 ,..., xn and an integer J. Return the count of unique numbers present in all windows whose size is J.Example: 1 arr = {5, 1, 2, 3, 2, 5, 9, 8, 0, 0, 0}; J = 3 Ans: {3, 3, 2, 3, 3, 3, 3, 2, 1} Explanation: All the windows of size J, which is 3, are: {5, 1, 2} ---> 3 unique numbers {1, 2, 3} ---> 3 unique numbers {2, 3, 2} ---> 2 unique numbers {3, 2, 5} ---> 3 unique numbers {2, 5, 9} ---> 3 unique numbers {5, 9, 8} ---> 3 unique numbers {9, 8, 0} ---> 3 unique numbers {8, 0, 0} ---> 2 unique numbers {0, 0, 0} ---> 1 unique number If we make a list of unique elements, we get {3, 3, 2, 3, 3, 3, 3, 2, 1}. FileName: UniqueNumbers.java Output: For the list: [5, 1, 2, 3, 2, 5, 9, 8, 0, 0, 0] The list of unique numbers are: [3, 3, 2, 3, 3, 3, 3, 2, 1] For the list: [5, 6, 7, 3, 3, 3, 1] The list of unique numbers are: [1, 1, 1, 1, 1, 1, 1] 26) An array arr containing only integers is given. Find 4 indices from the array arr such that arr[i] + arr[j] = arr[k] + arr[l] where i, j, k, &, l are the 4 indices. In the case of multiple answers, display any one set of 4 indices, which satisfies the given condition. Note that all the indices have to be unique.Example: 1 arr = {-2, 0, 2, 4, -2, -4}; Ans: {0, 1, 2, 5} Explanation: Values at the indices 0, 1, 2, and, 5 are -2, 0, 2, and, -4 respectively, and -2 + 0 = 2 + -4 => 2. Other answer can be {0, 2, 3, 5}, or {1, 2, 3, 4} FileName: EqualSum.java Output: For the list: [-2, 0, 2, 4, -2, -4] The list of indices are: [0, 1, 2, 5] For the list: [2, 4, 5, 9, -1, 15, 7] The list of indices are: [0, 1, 4, 6] 27) A string str and a string X are given. Look for the minimum window in str, which contains all the characters in X. The time complexity has to be linear. Note that the size of the window has to be equal to or greater than the size of string X.Example: 1 S = "APPLBEXODEBLBNC" X = "LNC" Ans: "LBNC" Explanation: Any other window will take more characters. The window of minimum size is mentioned in the answer. FileName: SmallestWindow.java Output: For the string: 'APPLBEXODEBLBNC' The smallest window is: LBNC 28) A one-dimensional integer array arr of length l is given. What is the length of the longest subsequence, which is first increasing and then decreasing?Example: arr = {2, 11, 3, 10, 5, 4, 3, 2, 1}; l = 9 Ans: 8 Explanation: The longest subsequence which is increasing and then decreasing is {2, 3, 10, 5, 4, 3, 2, 1}. There are 8 elements in it. Hence, the answer is 8. FileName: LongestSubsequence.java Output: For the list: [2, 11, 3, 10, 5, 4, 3, 2, 1] The length of subsequence which is increasing and then decreasing is: 8 For the list: [7, 6, 8, 10, 2, 5, 12, 30, 31, 20, 22, 18] The length of subsequence which is increasing and then decreasing is: 8 29) A set of coins X is given. Find out the number of ways one can make the sum S. Note that there is an infinite supply of the coins mentioned in the set X. Ensure that the space complexity is O(S). Note that ordering of coins should be ignored, i.e, 1 + 2 = 3 is one way; therefore, 2 + 1 = 3 won't be considered or vice versa. Also, set of coins will always be in ascending order.Example: X = {2, 3, 5, 7}; S = 15 Ans: 10 Explanation: There are 10 ways to get the sum S = 15. Following are those 10 ways: 2 + 2 + 2 + 2 + 2 + 2 + 3 = 15 2 + 2 + 2 + 3 + 3 + 3 = 15 2 + 2 + 2 + 2 + 2 + 5 = 15 2 + 3 + 5 + 5 = 15 2 + 2 + 3 + 3+ 5 = 15 2 + 2 + 2 + 2 + 7 = 15 2 + 3 + 3 + 7 = 15 3 + 3 + 3 + 3 + 3 = 15 3 + 5 + 7 = 15 5 + 5 + 5 = 15 FileName: CoinSum.java Output: For the set of coins: [2, 3, 5, 7] There are 10 ways to get the sum 15 For the set of coins: [4, 6, 8, 9, 10] There are 385 ways to get the sum 90 30) A chessboard of size R * C is given, where R represents the total number of rows, and C represents the total number of columns. Create a matrix arr, such that arr[x][y] represents the number of queen attacks occurring on the cell(x, y), assuming there is no queen present at the cell.Note: 1) Queen moves any number of squares at a time in the vertical direction, horizontal direction, or diagonally on any chessboard. A queen is not a knight; therefore, it can not jump over other queens. 2) An array of R strings is given, each of the size C. The string only contains '0' and '1'. 1 represents queen, and 0 means empty cell. Thus, at any cell, either a queen is present, or the cell is empty. 3) The worst time complexity can not exceed O(R * C). Example: Suppose the chessboard be [1 0 0] [0 0 1] [0 1 0] Where 1 is the queen position and 0 is empty. No queen attacks the cell(1, 1). Remember, we have to assume that no queen is present at the cell which we are evaluating for the attack. 3 attacks are coming from the queens to the cell (1, 2). One from the queen at the cell(1, 1), one from the queen at the cell(2, 3), and another from the cell(3, 2). Queens at (2, 3) and (1, 1) attack the cell(1, 3). Note that we have considered 1-indexing this time. Similarly, we can calculate the queen attack for other cells too. The final matrix is: [0, 3, 2] [3, 3, 1] [2, 1, 3] FileName: QueenAttack.java Output: For the chessboard: 1 0 0 0 0 1 0 1 0 The following matrix gives the number of queen attacks [0, 3, 2] [3, 3, 1] [2, 1, 3] |