This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package interviewQuestion; | |
//Use recursive approach | |
//Compare first and last characters if they are not same- return false | |
//If they are same make, remove the first and last characters and make a recursive call. | |
public class Palindrome { | |
public Boolean isPalindrome(String strX){ | |
if(strX.length()<2) return true; | |
if(strX.charAt(0)==strX.charAt(strX.length()-1)){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
public class SortUsingLastNames { | |
public void sortLast(ArrayList<String> al) { | |
Collections.sort(al, new Comparator<String>() { | |
@Override | |
public int compare(String o1, String o2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
public class SortUsingLastNames { | |
public void sortLast(ArrayList<String> al) { | |
Collections.sort(al, new Comparator<String>() { | |
@Override | |
public int compare(String o1, String o2) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.Queue; | |
public class T_BFS { | |
public void levelOrderQueue(Node root) { | |
Queue<Node> q = new LinkedList<Node>(); | |
if (root == null) | |
return; | |
q.add(root); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package GraphBFS; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
public class GraphBFS { | |
public static void main(String args[]) { | |
Graph g = new Graph(6); | |
g.addEdge(0, 1); | |
g.addEdge(0, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SecondLargestElement { | |
public static int findSecond(int[] A) { | |
int fstNo = A[0]; | |
int sndNo = -1; | |
for (int i = 1; i < A.length; i++) { | |
if (fstNo < A[i]) { | |
sndNo = fstNo;// store the first largest no value to second | |
// largest | |
fstNo = A[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
public class MinHeap_PQ { | |
PriorityQueue<Integer> pq; | |
public MinHeap_PQ() { | |
pq = new PriorityQueue<Integer>(); | |
} | |
public void insert(int[] x) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
import java.util.PriorityQueue; | |
public class MaxHeap_PQ { | |
PriorityQueue<Integer> pq; | |
public MaxHeap_PQ() { | |
pq = new PriorityQueue<Integer>(10, new Comparator<Integer>() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
public class KthSmallestElementInArray { | |
public static int find(int [] A, int k){ | |
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(); | |
for(int i=0;i<A.length;i++){ | |
pq.offer(A[i]); | |
} | |
int n = -1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NQueensBT { | |
public int[][] solution; | |
public NQueensBT(int N) { | |
solution = new int[N][N]; | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
solution[i][j] = 0; | |
} | |
} |
OlderNewer