Explore topic-wise InterviewSolutions in Current Affairs.

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.

How to check the equality of two arrays?

Answer»

You will be given two arrays and you have to check whether the 2 arrays are equal or not.

First, you have to check the lengths of two given arrays. When the length of both arrays is the same, we COMPARE corresponding elements of both arrays. Both the arrays will be considered equal If all corresponding pairs of elements are equal. If the arrays are big in size this method will be time-consuming therefore this method is not recommended to check the equality of two arrays. You can ALSO use the in-built equals() method of Arrays class but in the interview, the INTERVIEWER may ask you to compare two arrays without using in-built FUNCTIONS therefore this method will HELP you at that time.

2.

How do you merge two sorted arrays into one sorted array?

Answer»

During a programming interview, the employer may ask you to demonstrate your ability to work with more complex coding functions, such as merging arrays.

Approach 1:

  • To merge two arrays, you can create a new array of size equal to the sum of two arrays. After that, you can copy the ELEMENTS from the first array into the new one. Then you can traverse the second array and INSERT elements from it into the new one using insertion sort.This method takes
    • Time complexity: O(n1 * n2)
    • Extra Space: O(n1+n2) .

Approach 2 using Heap:

  • Convert the second array to min-heap:
    • As you traverse the first array, compare the current ELEMENT to the top of the created min_heap.
    • In this case, if the current element in the first array is greater than the heap top, swap the current element of the first array with the ROOT of the heap, and heapify the root of the min_heap.
    • The first array now contains the N first elements of the sorted merged array after PERFORMING the above operation for every element of the first array.
  • Now, the last M elements of the sorted merged array are in the min_heap or second array.
  • Apply in-place heapsort to the second array to sort them.
    • Time Complexity: O(N*logM + M*logN)
    • Extra Space: O(1)
3.

How can you get the index of an array element?

Answer»
  • You can find the index of an element through a linear or binary search. A linear search is a function in which you LOOP through each and EVERY element of an array until it finds the match of the desired element. When it finds the matching element, it returns the index. Therefore time COMPLEXITY of the linear search is O(n). Linear search can be APPLIED to sorted as WELL as an unsorted array.
  • If the array is sorted, you can use a binary search that repeatedly splits the array in half until the median of the interval matches the desired element and returns the index. Therefore time complexity of the binary search is O(log n).
4.

How do you remove a particular element from an array?

Answer»
  • You can't directly remove elements from the original array, as arrays are fixed sets and the SIZE can't change THEREFORE the interviewer is looking for you to suggest an ALTERNATE solution and address the issue that the question presents. The best way to remove an element WOULD be to create a new array. In this array, you could include copies of elements of the first array and omit only the element you want to remove.
  • Another approach is searching for the target element in the array and then moving all the elements in one position back which are on the RIGHT side of the target element.
5.

How do you find the missing integer in an array of range 1 to 100?

Answer»
  • During an interview, this question is often used to assess your knowledge of how programmers may manipulate or troubleshoot arrays. As the answer may depend on the exact elements or structure of the array, this question may also display your problem-solving abilities. In addition to showing your FLEXIBILITY and extensive knowledge, providing solutions to all situations can also impress the interviewer.
  • A missing integer can be found by CALCULATING the sum of the SERIES using this function: n (n + 1) / 2
  • This function will work only if the array doesn't contain any duplicates or is missing more than one number. If an array contains duplicate elements, you can SORT the array and determine whether there are two equal elements.
6.

Why is the complexity of fetching from an Array be O(1)?

Answer»

In an Array, objects are stored in continuous memory location. So, if you KNOW the address of the BASE OBJECT then you will be able to FIND the address of the ith object.

address(a[i]) = address(a[0]) + i*size(object)

This term is independent of n, so the time complexity of fetching from an Array is O(1).

7.

Compare Arrays with Linked Lists.

Answer»
  • Size: The size of an array cannot be ALTERED at runtime SINCE data can only be stored in contiguous blocks of memory in an array. However, due to the node structure of a linked list, its size can be altered easily since each node points to the next one such that data can exist at scattered (non-contiguous) addresses.
  • Memory ALLOCATION: For arrays, memory is allocated at compile time whereas for linked LISTS memory is allocated at runtime. But, a dynamically allocated array also allocates memory at runtime.
  • Memory efficiency: For the same number of elements, the linked list USES more memory due to its node structure since each node stores a reference to the next node along with the data. However, linked lists may use less memory overall compared to arrays when there is uncertainty about size or there are large variations in the size of data elements.
  • Execution time: In the case of a linked list, all the previous elements must be traversed to reach any element whereas elements in an array can be accessed directly using their index. As a result, some operations such as modifying an element are faster in arrays, while some other operations such as inserting an element are faster in linked lists.
8.

What do you mean by the terms “Dimension” and “Subscript” when we talk about arrays?

Answer»
  • In an array "Dimension" is the NUMBER of INDICES, or subscripts, that you need for SPECIFYING an individual element. Dimensions and subscripts may be confusing. 
  • A SUBSCRIPT is a number, while the dimension is a description of the range of acceptable keys.
  • You only need 1 subscript for each dimension of the array.
  • For example, arr[10][5] is an array having 2 dimensions:
    • One with size 10 and the other with size 5. You need 2 subscripts to address its elements. One between 0 and 9, inclusive; the other between 0 and 4.
Previous Next