This file contains hidden or 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 CustomList<T> { | |
| private Map<T, Value<T>> map = new HashMap<T, Value<T>>(); | |
| private List<Value<T>> list = new ArrayList<>(); | |
| private static final Random RANDOM = new Random(); | |
| public boolean add(T object){ | |
| Value<T> value = map.get(object); | |
| //This mean key does not exists | |
| if(value==null){ | |
| value = new Value<>(object,list.size()); |
This file contains hidden or 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 static <T> boolean instanceOf(T object, Class<?> clazz){ | |
| if(object==null){ | |
| return false; | |
| }else{ | |
| Class<?> baseClass = object.getClass(); | |
| if (baseClass.isArray() && clazz.isArray()) { | |
| return instanceOf(baseClass.getComponentType(), clazz.getComponentType()); | |
| } else | |
| return !(!baseClass.isArray() && clazz.isArray()) | |
| && !(baseClass.isArray() && !clazz.isArray()) |
This file contains hidden or 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
| /** Tree Node class **/ | |
| public class TreeNode<E> { | |
| private E data; | |
| private TreeNode<E> left; | |
| private TreeNode<E> right; | |
| public TreeNode(E data) { | |
| this.data = data; | |
| } |
This file contains hidden or 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 abstract class AbstractTreeIterator<E> implements Iterator<E> { | |
| protected AbstractTreeIterator(TreeNode<E> root){ | |
| assert(root!=null); | |
| } | |
| @Override | |
| public void remove() { | |
| throw new UnsupportedOperationException("remove is not supported"); | |
| } |
This file contains hidden or 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
| private static <T> boolean isSafe( | |
| final T array[][], | |
| final int rowIndex, | |
| final int colIndex, | |
| final boolean visited[][], | |
| final Predicate<T> predicate){ | |
| final int ROW = array.length; | |
| final int COL = array[0].length; |
This file contains hidden or 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.Arrays; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| public class KthElement { | |
| private static <T extends Comparable<T>> T getMedian(List<T> list){ | |
| Collections.sort(list); |
This file contains hidden or 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 static List<String> wordBreak(String str){ | |
| if(str==null){ | |
| throw new IllegalArgumentException("Invalid String"); | |
| } | |
| List<String> wordList = new ArrayList<>(); | |
| String subString=""; | |
| boolean isQuoteFound=false; | |
| for(char c:str.toCharArray()){ | |
| if(c!=' ' && c!='"'){ |
This file contains hidden or 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 static void generatePermutation(String str, final int index, final String prefix, List<String> list, | |
| final char[] allChars, final char replaceAbleChar){ | |
| if(str.equals("")){ | |
| list.add(prefix); | |
| }else{ | |
| char[] array = str.toCharArray(); | |
| if(array[index]!=replaceAbleChar){ | |
| generatePermutation(str.substring(index+1),0,prefix+array[index],list,allChars,replaceAbleChar); | |
| }else{ |
This file contains hidden or 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.Collections; | |
| import java.util.Iterator; | |
| import java.util.NoSuchElementException; | |
| import java.util.PriorityQueue; | |
| public class MedianIterator<T extends Comparable<T>> implements Iterator<T>{ | |
| private int size; | |
| private T[] array; | |
| private T median; | |
| private int currentIndex; |