Sunday, 10 September 2023

Java Most asked Coding/Programming Interview Questions

1. Find the first non-repeating character in a string.

package com.javaforsomeone.example;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

class Main {

    public static void main(String[] args) {
	  String name = "this is the java coding test";
	  firstNonRepeated(name);
	}

	public static void firstNonRepeated(String k) {

	Map hMap = new LinkedHashMap();
	for (char data : k.toCharArray()) {

		if (hMap.containsKey(data)) {
			hMap.put(data, hMap.get(data) + 1);
		} else {
			hMap.put(data, 1);
		}
	}
	Set data = hMap.keySet();
	for (Character a : data) {
	  if (hMap.get(a) == 1) {
		  System.out.println("First Non 
		  Repeated Character is " + a);
		  break;
	  }
	}

	}

}

Output: First Non Repeated Character is j

  

2. The sum of largest contiguous subarray.

package com.javaforsomeone.example;

public class Main {

	public static void main(String[] args) {

	  int[] arr = { -2, 1, -3, 4, -1, 2 };

	  System.out.println("The sum of contiguous subarray with the " 
	  + "largest sum is " + testMaxSum(arr));
	}

	private static Integer testMaxSum(int[] arr) {

	  int maxSum = 0;
	  int currentSum = 0;
	  for (Integer k : arr) {
	    currentSum = currentSum + k;
	    if (currentSum > maxSum) {
		    maxSum = currentSum;
	       } else if (currentSum < 0) {
		     currentSum = 0;
           }
		}
		return maxSum;
	}

}

Output: The sum of contiguous subarray with the largest sum is 5

  

3. Write a program to delete the Mid element from the Stack.

package com.javaforsomeone.example;

import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		
		Stack stack=new Stack();
		stack.push('A');
		stack.push('B');
		stack.push('C');
		stack.push('D');
		stack.push('E');
		System.out.println("The Stack after the deletion 
		of Mid Element "+deleteMid(stack));

	}
	
	public static Stack deleteMid(Stack st){
		
		int n=st.size();
		Stack tempStack =new Stack();
		int count=0;
		while(count<n/2) {
			char c =st.peek();
			st.pop();
			tempStack.push(c);
			count++;
		}
		st.pop();
		while(!tempStack.empty())
		{
			st.push(tempStack.peek());
			tempStack.pop();
		}
		return st;
	}

}

Output: The Stack after the deletion of Mid Element [A, B, D, E]
  

Sunday, 3 September 2023

Capco Java Interview Questions and answers

 Capco Java Interview questions and answers

1. Can you explain your roles and responsibilities in your current project.

2.  What are the coding advantages we have in coding using Java 8 over other below Java 8 versions
  • Functional Interfaces 
  • Lambda Expressions
  • forEach() Method In Iterable Interface
  • Optional Class
  • Default And Static Methods In Interfaces
  • Java Stream API For Bulk Data Operations On Collections
  • Java Date Time API
  • Collection API Improvements
  • Java IO Improvements
  • Miscellaneous Core API Improvements
  • Base64 Encode Decode
3. Give me an Example of you used Java 8 features in your project and significant advantage of it

4. Take a scenario of employee class has employee name, salary, manager attributes and we want to filter employee based on salary, later after some days we tell you to filter using employee name starting with some character. How would you design this class.

5. What do you mean by Dependency Injection?

6. How does Dependency Injection make the code loosely coupled?

7. Is it possible to do what Spring is doing like Dependency Injection by our self without using Spring?