0% found this document useful (0 votes)
2 views2 pages

Java Interview Coding Handbook

The document is a Java Interview Coding Handbook for candidates with 0-5 years of experience at Infosys, detailing core coding patterns with Java solutions. It includes examples for common problems such as Two Sum, Reverse String, Palindrome, Move Zeros, Maximum Subarray Sum, and more. Each coding pattern is presented with a concise Java implementation to aid in interview preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Interview Coding Handbook

The document is a Java Interview Coding Handbook for candidates with 0-5 years of experience at Infosys, detailing core coding patterns with Java solutions. It includes examples for common problems such as Two Sum, Reverse String, Palindrome, Move Zeros, Maximum Subarray Sum, and more. Each coding pattern is presented with a concise Java implementation to aid in interview preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Interview Coding Handbook (Infosys 0-5 YOE)

Core coding patterns with Java solutions.

Two Sum
public static int[] twoSum(int[] nums,int target){
Map<Integer,Integer> map=new HashMap<>();
for(int i=0;i<[Link];i++){
int diff=target-nums[i];
if([Link](diff)) return new int[]{[Link](diff),i};
[Link](nums[i],i);
}
return new int[]{};
}

Reverse String
String s="hello";
String rev=new StringBuilder(s).reverse().toString();

Palindrome
String s="madam";
boolean ans=[Link](new StringBuilder(s).reverse().toString());

Move Zeros
int j=0;
for(int i=0;i<[Link];i++)
if(arr[i]!=0){
int t=arr[i]; arr[i]=arr[j]; arr[j]=t; j++;
}

Maximum Subarray Sum (Kadane)


int cur=nums[0], max=nums[0];
for(int i=1;i<[Link];i++){
cur=[Link](nums[i],cur+nums[i]);
max=[Link](max,cur);
}

Max Sum Subarray Size K


int sum=0;
for(int i=0;i<k;i++) sum+=arr[i];
int max=sum;
for(int i=k;i<[Link];i++){
sum+=arr[i]-arr[i-k];
max=[Link](max,sum);
}

Longest Substring Without Repeating


Set<Character> set=new HashSet<>();
int l=0,max=0;
for(int r=0;r<[Link]();r++){
while([Link]([Link](r))) [Link]([Link](l++));
[Link]([Link](r));
max=[Link](max,r-l+1);
}

Binary Search
int l=0,r=[Link]-1;
while(l<=r){
int m=l+(r-l)/2;
if(arr[m]==target) return m;
if(arr[m]<target) l=m+1; else r=m-1;
}

Reverse Linked List


ListNode prev=null,curr=head;
while(curr!=null){
ListNode next=[Link];
[Link]=prev;
prev=curr;
curr=next;
}

Valid Parentheses
Stack<Character> st=new Stack<>();
for(char c:[Link]()){
if(c=='(') [Link](c);
else if([Link]()) return false;
else [Link]();
}

You might also like