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 BFS{ | |
public static Node[] prev; | |
public static Graph G; | |
public static void BFSWithBackTracking( ) | |
{ | |
if(G==null) | |
return; | |
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
/** | |
Good morning! Here's your coding interview problem for today. | |
This problem was asked by Yahoo. | |
Recall that a full binary tree is one in which each node is either a leaf node, or has two children. Given a binary tree, convert it to a full one by removing nodes with only one child. | |
For example, given the following tree: |
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
/* | |
Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to | |
its right side. And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, | |
leaders are 17, 5 and 2. | |
*/ | |
//Approach- Scan from Right to Left . last element is always leader. | |
void printLeaeders(int[] iinput){ | |
if (input== null) |
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
/** | |
* Sorting approach | |
* Sort each letter from the input.Keep sorted word as the key with a list of strings that are anagrams. | |
**/ | |
public void groupAnagrams( String in) | |
{ | |
if (str==null) | |
return; | |
HashMap<String,List<String>> map = new HashMap<String,List<String>>(); |
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 HashMap<K,V> | |
{ | |
private double loadFactor = 0.75; | |
private Entry[] table; | |
private int elemCount;; | |
public static class Entry | |
{ | |
K key; | |
V value; |
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
/* | |
Design a stack that supports push, pop, and retrieving the minimum element in constant time. Can you do this? | |
Solution: | |
Keep a stack of minimums: Extra memory | |
*/ | |
public class StackWithMin{ |
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
// Logical extension to hare-tortoise cycle detection algorithm | |
// first check for cycle using rabbit-tortoise algo then move back tortoise to head and advance both rabbit and | |
//tortoise one by one | |
// The point where they meet will be the start of the loop | |
/** | |
* Returns the node at the start of a loop in the given circular linked | |
* list. A circular list is one in which a node's next pointer points | |
* to an earlier node, so as to make a loop in the linked list. For | |
* instance: | |
* |
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.Arrays; | |
/** | |
* quickselect is a selection algorithm to find the kth smallest element in an | |
* unordered list. Like quicksort, it is efficient in practice and has good | |
* average-case performance, but has poor worst-case performance. Quickselect | |
* and variants is the selection algorithm most often used in efficient | |
* real-world implementations. | |
* | |
* Quickselect uses the same overall approach as quicksort, choosing one |
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.Arrays; | |
/** | |
* Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n | |
* that satisfy the condition nums[i] + nums[j] + nums[k] < target. | |
* <p> | |
* For example, given nums = [-2, 0, 1, 3], and target = 2. | |
* Return 2. Because there are two triplets which sums are less than 2: | |
* <p> | |
* [-2, 0, 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
/** | |
Design and implement a TwoSum class. It should support the following operations: add and find. | |
add - Add the number to an internal data structure. find - Find if there exists any pair of numbers which sum is equal to the value. | |
For example, | |
add(1); add(3); add(5); | |
find(4) -> true | |
find(7) -> false | |
**/ |
NewerOlder