Bajaj Finserv Coding Questions

 

Introduction

Bajaj Finserv Limited is an Indian non-banking financial services company headquartered in Pune. It is focused on lending, asset management, wealth management and insurance. This article will discuss some of the most important bajaj finserv coding questions that are asked in the Bajaj Finserv recruitment process. 

Question 1

Let's have a look at the first coding question for Bajaj Finserv. We will discuss the problem statement and then its solution.

Problem Statement

Write a program that checks whether there are two entries in the array A[] whose total is exactly x, given an array A[] of n numbers and another number x.

TestCases

Below are some of the example test cases shown to explain the problem:

Example 1

Input: 

arr[] = {0, -1, 2, -3, 1}

x= -2


Output: 

YES

Example 2

Input: 

arr[] = {1, -2, 1, 0, 5}

x = 0


Output: 

NO

Solution

Using the two-pointer technique to solve this problem can be difficult. However, the array must be sorted before employing two-pointer approaches.


After the array has been sorted, the two pointers at the beginning and end of the array can be taken.


Shift the right pointer to decrease the value of the necessary sum if the sum is larger than the sum of those two elements, and shift the left pointer to increase the value of the needed sum if the sum is less than the required value.


Algorithm:

  1. hasSum (A[], ar_size, sum)

  2. Sort the array in non-decreasing order.

  3. Initialize two index variables to find the candidate 

  4. elements in the sorted array. 

    1. Initialize first to the leftmost index: l = 0

    2. Initialize second the rightmost index: r = ar_size-1

  5. Loop while l < r. 

    1. If (A[l] + A[r] == sum) then return 1

    2. Else if( A[l] + A[r] < sum ) then l++

    3. Else r–-

  6. No candidates in the whole array – return 0


Analysis of Complexity:

  • Time complexity varies depending on the sorting algorithm used.

  • If Merge Sort or Heap Sort is employed, the worst scenario is (-)(nlogn).

  • If Quick Sort is utilised, the worst-case time is O(n2).

  • Auxiliary Space is also affected by the sorting process. For merge sort, the auxiliary space is O(n), but for heap sort, it is O(1).

Code

The following is how the above approach is put into action:


import java.util.*;

 

public class Main {

    static boolean hasSum(int[] A, int arr_size, int sum){

        int l, r;

        Arrays.sort(A);

        l = 0;

        r = arr_size - 1;

        while (l < r) {

            if (A[l] + A[r] == sum) {

                return true;

            } else if (A[l] + A[r] < sum) {

                l++;    

            } else {

                r--;

            }

        }

        return false;

    }

 

    public static void main(String args[]) {

        int A[] = { 1, 2, 45, 6, 10, -8 };

        int n = 16;

        int arr_size = A.length;


        if (hasSum(A, arr_size, n)) {

            System.out.println("YES");

        } else {

            System.out.println("NO");

        }

    }

}

Question 2

Let us have a look at the second coding question for Bajaj Finserv. We will discuss the problem statement and then its solution.

Problem Statement

Use the following algorithm to compress an array of characters, chars:


Begin with an empty string s. In chars, for each group of successive repeating characters:

If the group is only one character long, attach the character to s.

Otherwise, attach the character and the length of the group.


Instead of returning the compressed string s separately, it should be placed in the


chars input character array Group lengths of 10 or more characters will be broken into numerous characters in chars.


Return the updated length of the input array after you've finished altering it.

Solution

It might be a little difficult to start with an in-place solution. So, to solve this problem, we first think about the non-in-place solution.



We use a new array ans, two pointers i & j, and a counter.

  1. If the chars[i] == chars[i-1], then we add up the counter by 1.

  2. Otherwise, we have to compress the string with the counter.

  3. 2.1 If the counter == 1, it means we don't need to add the number in ans.

  4. 2.2 If the counter > 1, we add the counter as a string in ans.


class Solution {

    public int compress(char[] chars) {

        char[] ans = new char[chars.length];

        int cnt = 1, j = 0;

        for(int i = 1; i < chars.length + 1; i++){

            if(i < chars.length && chars[i] == chars[i-1]) cnt++;

            else{

                ans[j++] = chars[i-1];

                if(cnt != 1){

                    for(char c : String.valueOf(cnt).toCharArray()) ans[j++] = c;

                    cnt = 1;

                }

            }

        }

        // Overwrite to the original array

        for(int i = 0; i < j; i++) chars[i] = ans[i];

        return j;

    }

}


Then, we find that because the last index of ans (i.e., j) is always smaller than index i, so we don't need a new array. Instead, we overwrite the original array. So, in the code, we simply use chars instead of ans.


class Solution {

    public int compress(char[] chars) {

        int j = 0;

        for(int i = 1, cnt = 1; i <= chars.length; i++){

            if(i < chars.length && chars[i] == chars[i-1]) cnt++;

            else{

                chars[j++] = chars[i-1];

                if(cnt != 1){

                    for(char c : String.valueOf(cnt).toCharArray()) chars[j++] = c;

                    cnt = 1;

                }

            }

        } 

        return j;

    }

}



Complexity Analysis:

Time complexity: O(n)

Space Complexity: O(n)

Conclusion

We have extensively discussed the solution to the coding questions asked in Bajaj Finserv. The article explains the approach which is used in the above-given problems.


Comments

Popular posts from this blog

What is the AGGRCOW Problem?

Advantages and Limitations of Using Recursion in Programming

How to use Python code online?