Raja Software Labs interview questions

Raja Software Labs Interview Questions (RSL)

1. Occurrence of character:–

import java.util.*;
public class Main
{
	public static void main(String[] args) {
	  Scanner sc = new Scanner(System.in);
	  String str = sc.next();
	  char b='a';
	  
	  boolean flag=false;
	  for(int i=0;i<str.length();i++)
	  {
	      if(str.charAt(i)==b)
	      {
	          flag=true;
	          System.out.println("char find");
	      }
	  }
	  if(flag==false)
	  {
	      System.out.println("char not find");
	  }
	}
}

2. Integer to roman: —

class Solution {
    public int romanToInt(String s) {
        int n = s.length();
        
        int sum=0;
        for(int i=0;i<n;i++)
        {
            if(s.charAt(i)=='M')
            {
                sum+=1000;
            }
            else if(s.charAt(i)=='D')
            {
                sum+=500;
            }
            else if(s.charAt(i)=='C')
            {
                if(i<n-1 && s.charAt(i+1)=='D')
                {
                    sum+=400;
                    i++;
                }
                else if(i<n-1 && s.charAt(i+1)=='M')
                {
                    sum+=900;
                    i++;
                }
                else{
                    sum+=100;
                }
            }
            else if(s.charAt(i)=='L')
            {
                sum+=50;
            }
            else if(s.charAt(i)=='X')
            {
                if(i<n-1 && s.charAt(i+1)=='L')
                {
                    sum+=40;
                    i++;
                }
                else if(i<n-1 && s.charAt(i+1)=='C')
                {
                    sum+=90;
                    i++;
                }
                else{
                    sum+=10;
                }
            }
            else if(s.charAt(i)=='V')
            {
                sum+=5;
            }
            else if(s.charAt(i)=='I')
            {
                if(i<n-1 && s.charAt(i+1)=='V')
                {
                    sum+=4;
                    i++;
                }
                else if(i<n-1 && s.charAt(i+1)=='X')
                {
                    sum+=9;
                    i++;
                }
                else{
                    sum+=1;
                }
            }
        }
        return sum;
    }
}

3. Given sum and we have to print all the subsequences in array with given sum:–

public class Main {
    // To find sum of all subsequences
    static int findSum(int arr[], int n)
    {
        // Sum all array elements
        int sum = 0;
        for (int i = 0; i < n; i++)
            sum += arr[i];
 
        // Result is sum * 2^(n-1)
        return sum * (1 << (n - 1));
    }
 
    // Driver program to test findSum()
    public static void main(String[] args)
    {
        int arr[] = { 1, 2 };
        int n = arr.length;
        System.out.print(findSum(arr, n));
    }
}

4. Sort array of 0s and 1s in only one loop solution was using two pointers:–

import java.util.*;
import java.lang.*;
import java.io.*;
​
public class Main
{
   public static void main(String[] args){
​
     Scanner sc= new Scanner(System.in);
     int n= sc.nextInt();
     int arr[]= new int[n];
     
     for(int i=0; i<n; i++){
         arr[i]= sc.nextInt();
     }
     
     int i=0, j=n-1;
     
     while(i<j){
         if(arr[i]==0 && arr[j]== 1){
             int temp =arr[i];
             arr[i]= arr[j];
             arr[j]= temp;
             i++;
             j--;
         }
         else if(arr[i]==1) i++;
         else if(arr[j]==0) j--;
         
     }
     
     for(int num : arr){
     System.out.print(num + " ");
     }
      
	}
}

5. Print string without taking repeating characters eg helper – helpr:–

import java.util.*;
import java.lang.*;
import java.io.*;
 
public class Main
{
   public static void main(String[] args){
 
     Scanner sc= new Scanner(System.in);
     String s= sc.next();
     ArrayList<Character> list= new ArrayList<>();
     String ans = "";
     
     for(int i=0; i<s.length(); i++){
         
             if(!list.contains(s.charAt(i))){
             
              ans += s.charAt(i);
              list.add(s.charAt(i));
         }
         
     }
     System.out.println(ans);
     
    }
}

Or

import java.util.*;
import java.lang.*;
import java.io.*;
​
public class Main
{
   public static void main(String[] args){
​
     Scanner sc= new Scanner(System.in);
     String s= sc.next();
     boolean arr[]= new boolean[26];
     String ans= "";
     
     for(int i=0; i<s.length(); i++){
         
         if(arr[s.charAt(i)-'a']==false) {
             
             ans+= s.charAt(i);
             
             arr[s.charAt(i)-'a']=true;
         }
     }
     System.out.println(ans);
      
	}
}

6.

Print 2324

as 2000

300

20

4

import java.util.*;
public class Main
{
	public static void main(String[] args) {
		System.out.println("Hello World");
		ArrayList<Integer> list= new ArrayList<Integer>();
		int num= 2324;
		int factor= 1;
	            Int d= 0;
	    
		
		while(num!=0) {
		    
		    d= num%10;
		    d=d*factor;
		    
		    list.add(d);
		    
		    factor*= 10;
		    
		    num/=10;
		}
		Collections.reverse(list);
		
		for(int a : list) {
		    System.out.println(a);
		}
	}
	
}

7. First non-repeating character from a string:–

import java.util.*;
public class Main {
    public static void main(String args[]) {
     
        Scanner sc = new Scanner(System.in);
        String str =sc.next();

        int n =str.length();
        for(int i=0;i<n;i++)
        {
            
            if(str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i)))
            {
                System.out.println("First non-repeating character is: "+str.charAt(i));
                break;
            }
        }

        // for(char i :str.toCharArray()){
        // if ( str.indexOf(i) == str.lastIndexOf(i)) {
        //     System.out.println("First non-repeating character is: "+i);
        //     break;
        // }
        // }
    }
}

8. Print triangle pattern of stars:–

class Main
{
	public static void main(String[] args)
	{
		// n is number of rows
		final int n = 5;

		// Do for each row
		for (int i = 1; i <= n; i++)
		{
			// Print '*' i times
			for (int j = 1; j <= i; j++) {
				System.out.print('*');
			}

			// Move to the next line
			System.out.print(System.lineSeparator());
		}
	}
}

9. 66 plus one:–

class Solution {
    public int[] plusOne(int[] digits) {
        for (int i = digits.length -1 ; i >= 0; i-- ){
            if (digits[i] < 9){
                digits[i]++;
                return digits;
            }
            digits[i] = 0;
        }
        
        int[] result = new int[digits.length + 1];
        result[0] = 1;
        return result;
    }
}

10. Replace space with %20 in string:–

public class Main
{
	public static void main(String[] args) {
		String str="he h p d";
		String brr=str.replace(" ","%20");
		System.out.println(brr);
	}
}

11. Second max in array using single loop :–

class Solution {
    int print2largest(int arr[], int n) {
        // code here
        
      Arrays.sort(arr);
      int temp = 0;
      for(int i=n-2;i>=0;i--)
      {
          if(arr[i] != arr[n-1])
          {
             temp = arr[i];
             return temp;
          }
         
      }
      return -1;
    }
}

Or

class Solution {
    int print2largest(int arr[], int n) {
        // code here
        int firstMax = -1;
        int secondMax = -1;
        
        for(int i=0;i<n;i++) {
            if(arr[i]>firstMax) {
                secondMax = firstMax;
                firstMax = arr[i];
            }
            if(arr[i]>secondMax && arr[i]!=firstMax) {
                secondMax = arr[i];
            }
        }
        
        return secondMax;
    }
}

12. Leep Year :–

public class Main {

  public static void main(String[] args) {

    // year to be checked
    int year = 1900;
    boolean leap = false;

    // if the year is divided by 4
    if (year % 4 == 0) {

      // if the year is century
      if (year % 100 == 0) {

        // if year is divided by 400
        // then it is a leap year
        if (year % 400 == 0)
          leap = true;
        else
          leap = false;
      }
      
      // if the year is not century
      else
        leap = true;
    }
    
    else
      leap = false;

    if (leap)
      System.out.println(year + " is a leap year.");
    else
      System.out.println(year + " is not a leap year.");
  }
}

13. Reverse a word:–

//Reverse Words in a String

class Main{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        char arr[] = str.toCharArray();
        int n = str.length();
        int start = 0;
        int end = n-1;
        
        while(start<end)
        {
            char temp=arr[start];
            arr[start]=arr[end];
            arr[end]=temp;
            start++;
            end--;
        }
        
        for(int i=0;i<n;i++)
        {
            System.out.print(arr[i]);
        }
    }
}

14. Fibonacci Member:–

import java.util.*;
class Main{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int i=1;
        int a=0,b=1;
        
        if(a==n)
        {
           System.out.println("number comes under fibonacii sequence");
           return;
        }
        
        if(b==n)
        {
           System.out.println("number comes under fibonacii sequence");
           return;
        }
        
        while(i<n)
        {
            int sum=a+b;
            a=b;
            b=sum;
            i++;
           if(n==sum)
           {
               System.out.println("number comes under fibonacii sequence");
               break;
           }

           else{
               if(i==n)
               {
                   System.out.println("not under sequnce");
               }
           }
        }
    }
}

15. Anagram in o(n):–

class Solution {
    public boolean isAnagram(String s, String t) {
        
        if(s.length() != t.length()) return false;
        
        int countS[]= new int[26];
        int countT[]= new int[26];
        
        for(int i=0; i<s.length(); i++){
            
            countS[s.charAt(i)-'a']++;
        }
        
        for(int i=0; i<t.length(); i++){
            
          countT[t.charAt(i)-'a']++;
                
        }
        
        for(int i=0; i<26; i++){
            
            if(countS[i] != countT[i]) return false;
        }
        
        return true;
        
    }
}

Or

class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.length() != t.length()){
            return false;
        }
        int[] arr = new int[26];
        
        for(int i = 0;i<s.length();i++){
            arr[s.charAt(i)-'a']++;
        }
        
        for(int i = 0 ;i<t.length();i++){
            arr[t.charAt(i)-'a']--;
        }
        
        for(int i = 0;i<arr.length;i++){
            if(arr[i]!=0){
                return false;
            }
        }
        return true;
    }
}

16. Maximum Difference :—

class Solution
{
    int findMaxDiff(int a[], int n)
    {
	   int[] LS= new int[n];
	   int[] RS = new int[n];
	   
	   LS[0]=0;
	   
	   for(int i=1;i<n; i++) {
	       
	       int j= i-1;
	       while(j>=0){
	           
	           if(a[j]<a[i]){
	               LS[i]= a[j];
	               break;
	           }
	           j--;
	       }
	       }
	   
	  RS[n-1]= 0;
	  
	   for(int i=n-2; i>=0; i--){
	       int j=i+1;
	       
	       while(j<n){
	           
	           if(a[i]>a[j]){
	               
	               RS[i]= a[j];
	               break;
	           }
	           j++;
	       }
	   }
	   
	   int maxDiff= -1;
	   
	   for(int i=0; i<n; i++){
	       
	       int diff= Math.abs(LS[i]-RS[i]);
	       
	        maxDiff= Math.max(maxDiff, diff);
	   }
	   return maxDiff;
    }
}

17. Remainder when divided by 11:–

// JAVA implementation to find remainder
// when a large number is divided by 11
import java.io.*;

class Main{
	
	// Function to return remainder
	static int remainder(String str)
	{
		// len is variable to store the
		// length of number string.
		int len = str.length();
	
		int num, rem = 0;
	
		// loop that find remainder
		//(str.charAt(i) - '0') return interger value of str.charAt(i)
		for (int i = 0; i < len; i++) {
			num = rem * 10 + (str.charAt(i) - '0');
			rem = num % 11;
		}
	
		return rem;
	}
	
	// Driver code
	public static void main(String args[])
	{
		String str = "3435346456547566345436457867978";
		System.out.println(remainder(str));
	}
}

18. Glass Hour :–

import java.util.*;
import java.lang.*;
import java.io.*;
​
public class Main
{
  
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc= new Scanner(System.in);
        int N =sc.nextInt();
        int M= sc.nextInt();
        int[][] Mat= new int[N][M];
​
       for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            
           Mat[i][j]= sc.nextInt();
        }
       }
​
      int max_sum= 7;
        
        
        if(N<3 || M<3) {
            System.out.println(-1);
            return;
        }
        
        for(int i=0; i<N-2; i++){
           
            int curr_sum1= 0;
            int curr_sum2 =0;
            int curr_sum3 =0;
            int curr_sum = 0;
            
            for(int j=0; j<M-2; j++){
                
                curr_sum1 = Mat[i][j] + Mat[i][j+1] + Mat[i][j+2];          //    a b c    (a+b+c)
                
                curr_sum2= Mat[i+1][j+1];                                   //      d        (d)
                
                curr_sum3= Mat[i+2][j] + Mat[i+2][j+1] + Mat[i+2][j+2];      //   e f g     (e+f+g)
                
                curr_sum= curr_sum1 + curr_sum2 + curr_sum3;
                
                max_sum = Math.max(max_sum, curr_sum);
​
            }
        }
        
        System.out.println(max_sum);
	}
}

19. Program to check if a matrix is symmetric:–

// Simple java code for check a matrix is
// symmetric or not.

import java.io.*;

class GFG {

static int MAX = 100;

// Fills transpose of mat[N][N] in tr[N][N]
static void transpose(int mat[][], int tr[][], int N)
{
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			tr[i][j] = mat[j][i];
}

// Returns true if mat[N][N] is symmetric, else false
static boolean isSymmetric(int mat[][], int N)
{
	int tr[][] = new int[N][MAX];
	transpose(mat, tr, N);
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			if (mat[i][j] != tr[i][j])
				return false;
	return true;
}

// Driver code
	public static void main (String[] args)
{
		
		int mat[][] = { { 1, 3, 5 },
					{ 3, 2, 4 },
					{ 5, 4, 1 } };

	if (isSymmetric(mat, 3))
		System.out.println( "Yes");
	else
		System.out.println ( "No");
	
	}
}

20.

Implement the below function to print the count of numeric characters(0-9) in a given string and also print the count of remaining characters. (ignore the characters cases, it can be lower or upper case)

The function will take 1 parameter which will be string (or a character array). You do not have to write code to get the input parameter from the user.

Example:–

Input : raja1123labs

Output: ‘1’=2, ‘2’=1, ‘3’=1, others=8

Input: Buffet101

Output: ‘1’=2, ‘0’=1, others=6

 import java.util.*;
public class Main
{
 public static void main(String[] args) {
     
     String s="raja1123labs";
     int n= s.length();
     int count[]= new int[10];
     
     
     for(int i=0; i<n; i++){
         
         if(s.charAt(i)>= '0' && s.charAt(i)<='9'){
             
             count[s.charAt(i)-'0']++;
         }
     }
     
     int sum=0;
     
     for(int i=0; i<count.length; i++){
        sum+= count[i];
         
         if(count[i]>0){
             System.out.print(i + "=" + count[i] + " ");
         }
     }
     System.out.print("others" + " " + (n-sum) );
 }}

21.

Write a function that takes an input parameter as a String. The function should replace the alternate words in it with “zyz” and print it. Words are separated by dots. (Avoid using inbuilt functions)

If input is “i.like.this.program.very.much”

Output will be “i.xyz.this.xyz.very.xyz”

import java.util.*;
class Main {
   
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a String: ");
       
        String s = in.nextLine();
        int length = s.length();
       
        String word = "";
        int position = 0;
       
        for (int i = 0; i < length; i++)
        {
            char c = s.charAt(i);
            if (c != '.')
            {
                word = word + c;
            }
            else {
                position++;
                if (position % 2 == 0) {
                    System.out.print("xyz.");
                } else {
                    System.out.print(word + ".");
                }


                word = "";
            }
           
            if (i == length - 1)
            {
                position++;
                if (position % 2 == 0)
                {
                    System.out.print("xyz");
                }
                else
                {
                    System.out.print(word);
                }
            }
           
        }
        in.close();
    }
}

22. Write a function that takes an array of integers as input and prints the second-maximum difference between any two elements from an array.

Example:–

Int arr[]={14,12, 70,15, 95, 65, 22, 30};

First max-difference=95-12=83

Second max-difference=95-14=81

So output should be 81

import java.util.*;
class Main{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int count[] = new int[n];
       
        for(int i=0;i<n;i++)
        {
            count[i]=sc.nextInt();
        }
       
        Arrays.sort(count);
       
        int sec_max_diff=count[n-1]-count[1];
        System.out.println(sec_max_diff);
    }
}

23. Write a function that takes input as an integer number and prints the closest prime integer to that number. The closest prime can be greater or smaller than the passed input integer. If there are equidistant prime-numbers, print both.

Example:

input#1: 32

output#1: 31

input#2: 30

output#2: 31

import java.util.*;
class Main{
    public static void main(String args[])
    {
        Scanner sc= new Scanner(System.in);
     int n= sc.nextInt();
     
     int left=0, right=0;
     
     for(int i=n; i>=2; i--){
         boolean prime= true;
         
         for(int j=2; j<i-1; j++){
             
             if(i%j == 0){
                 prime= false;
             }
         }
         if(prime){
             
             left = i;
             break;
         }
     }
     
     for(int i=n; ; i++){
         boolean prime= true;
         
         for(int j=2; j<i-1; j++){
             
             if(i%j == 0){
                 prime= false;
             }
         }
         if(prime){
             
             right = i;
             break;
         }
     }
     
        if(n-left == right-n){
           
            System.out.println(left + " " + right);
        }
       
        if(n-left < right-n){
            System.out.println(left);
        }
        else{
            System.out.println(right);
        }
    }
}

24.

5. Variation of Fibonacci

The Fibonacci sequence is constructed by adding the last two numbers of the sequence so far to get the next number in the sequence. The First and the second numbers of the sequence are defined as 0 and 1.

We get:

0, 1, 1, 2, 3, 5, 8, 13, 21….

Write a function which takes input as a number:

  • If the given number is Fibonacci number, print the number
  • If the given number is NOT a fibonacci number, print the sum of all Fibonacci numbers less than the given number.

Example:

{12 is Fibonacci number}

input : 21 

Output: 21

{20 is Not a Fibonacci number so}

Output is 10(13+8+5+3+2+1+1)

input : 20

output: 33

import java.util.*;
class Main{
    public static void main(String[] args)
    {
     Scanner sc= new Scanner(System.in);
     int n= sc.nextInt();
     
     int first=0, sec=1;
     int new_num=0;
     int sum=0;

     for(int i=1; ; i++){
         
         sum += first;
         
         new_num= first + sec;
         
         if(n == first){
             System.out.println(n);
             break;
         }
         if(first> n){
             
             System.out.println(sum-first);
             break;
         }
         
         first= sec;
         sec= new_num;
         }
     }
     }

Technical Round Questions:–

  1. Stack : https://www.geeksforgeeks.org/stack-data-structure-introduction-program/
  2. Insertion Sort : https://www.geeksforgeeks.org/insertion-sort/
  3. Bubble Sort : https://www.geeksforgeeks.org/bubble-sort/
  4. Leap Year : https://www.geeksforgeeks.org/program-check-given-year-leap-year/
  5. Find Second largest element in an array : https://www.geeksforgeeks.org/find-second-largest-element-array/
  6. Convert string to integer without using any in-built functions : https://www.geeksforgeeks.org/convert-string-to-integer-without-using-any-in-built-functions/
  7. Merge two sorted arrays with O(1) extra space : https://www.geeksforgeeks.org/merge-two-sorted-arrays-o1-extra-space/
  8. Print Prime Numbers : https://codescracker.com/cpp/program/cpp-program-print-prime-numbers.htm
  9. Length of the Longest Consecutive 1s : https://www.geeksforgeeks.org/length-longest-consecutive-1s-binary-representation/
  10. First Non Repeating Character in a String : https://www.geeksforgeeks.org/given-a-string-find-its-first-non-repeating-character/
  11. Check if a matrix is symmetric : https://www.geeksforgeeks.org/program-to-check-if-a-matrix-is-symmetric/
  12. Check if strings are rotations of each other or not : https://www.geeksforgeeks.org/a-program-to-check-if-strings-are-rotations-of-each-other/
  13. Find the fastest 3 horses : https://www.geeksforgeeks.org/puzzle-9-find-the-fastest-3-horses/
  14. Jump in rank of a student after updating marks : https://www.geeksforgeeks.org/jump-in-rank-of-a-student-after-updating-marks/
  15. Stock Buy Sell to Maximize Profit : https://www.geeksforgeeks.org/stock-buy-sell/

Add a Comment

Your email address will not be published. Required fields are marked *