This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Find out smallest and largest number in a given Array? |
|
Answer» Approach:
Output : Largest and Smallest numbers are 77 10ConclusionIn this article, we have covered the most IMPORTANT and commonly asked interview questions based on arrays. To make the most of all the knowledge AVAILABLE, it is absolutely necessary to practice data structures and algorithms as much as POSSIBLE. You should keep in mind certain properties of array data structures, for example, array index starts at 0, the elements of an array are stored in contiguous MEMORY locations, etc. Additional Interview Resources:
|
|
| 2. |
Check for balanced parenthesis in an expression using constant space. |
Answer»
Approach:
|
|
| 3. |
Two sum of an array: In this question you will be given an array arr and a target. You have to return the indices of the two numbers such that they add up to target. |
|
Answer» In this question, you will be given an array arr and a target. You have to return the indices of the two NUMBERS such that they add up to the target. Approach: So the easiest and most efficient solution is using a HashMap. We have to iterate through all the elements of the array and if (target-num) is present in the array then return {lower index, higher index} else PUSH the value at arr[i] and the index i in the hashmap. public class InterviewBit { public int[] twoSum(int[] arr, int target) { HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>(); for(int i = 0; i <arr.length; i++){ Integer diff = (Integer)(target - arr[i]); if(hash.containsKey(diff)){ int TORETURN[] = {hash.get(diff)+1, i+1}; return toReturn; } hash.put(arr[i], i); } return NULL; }}The time Complexity for this approach will be O(n). |
|
| 4. |
How do you rotate an array? |
|
Answer» You will be given an input number N and N numbers following it as the input. You are ALSO given an integer K which represents how many times you have to rotate the array. Rotate the array K values to the right if K is positive and K values to the left if K is negative. If K=0, do not rotate the array. The figure shows how the rotating array will look if k=2. Approach:
Code: import java.io.*;import java.util.*;public class Main { public static void display(int[] arr) { StringBuilder sb = new StringBuilder();for (int val : arr) { sb.append(val + " "); } System.out.println(sb); } public static void reverse(int[] arr, int li, int ri) { while (li < ri) { int t = arr[li]; arr[li] = arr[ri]; arr[ri] = t; li++; ri--; } } public static void rotate(int[] arr, int k) { k = k % arr.length; if (k < 0) { k += arr.length; } reverse(arr, 0, arr.length - k - 1); reverse(arr, arr.length - k, arr.length - 1); reverse(arr, 0, arr.length - 1); } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(br.readLine()); } int k = Integer.parseInt(br.readLine()); rotate(arr, k); display(arr); } |
|
| 5. |
How do you sort an array of 0 and 1? |
|
Answer» You are given an array of 0s and 1s in random order and you have to sort this array i.e Segregate 0s on the left side and 1s on the right side of the array. Approach:
Time Complexity : O(n) |
|