Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
KodeSeeker / BFSWithBackTracking.java
Last active March 3, 2024 07:54
Breadth first search with BackTracking.
public class BFS{
public static Node[] prev;
public static Graph G;
public static void BFSWithBackTracking( )
{
if(G==null)
return;
@KodeSeeker
KodeSeeker / SingleChildTrimmer.java
Created July 4, 2023 19:47
Daily Coding Problem #1444
/**
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:
@KodeSeeker
KodeSeeker / LeadersinArray.java
Last active December 19, 2022 05:32
Find leaders in an array. ie. Print element if no element greater than it exists on the right.
/*
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)
@KodeSeeker
KodeSeeker / AnagramsGroup.java
Created August 8, 2014 03:34
Group all Anagrams in Stream of Input Words
/**
* 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>>();
@KodeSeeker
KodeSeeker / HashMap.java
Created April 30, 2013 18:36
Custom HashMap Implementation
public class HashMap<K,V>
{
private double loadFactor = 0.75;
private Entry[] table;
private int elemCount;;
public static class Entry
{
K key;
V value;
@KodeSeeker
KodeSeeker / ConstantStackMinimum.java
Last active April 12, 2020 20:02
Stack that Support Push, Pop, and GetMin in Constant Time
/*
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{
@KodeSeeker
KodeSeeker / StartNodeCircularlyLinkedList.java
Created September 30, 2013 00:26
Find the starting node of a loop in a circularly linked list. **Important**
// 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:
*
@KodeSeeker
KodeSeeker / QuickSelect.java
Created January 6, 2019 19:29 — forked from unnikked/QuickSelect.java
A basic implementation of quickselect algorithm in Java. Intentionally unused generics.
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
@KodeSeeker
KodeSeeker / ThreeSumSmaller.java
Created September 30, 2018 22:25
ThreeSumSmaller.java
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]
@KodeSeeker
KodeSeeker / TwoSumFileStructure.java
Created September 29, 2018 22:31
Design and implement a TwoSum class. It should support the following operations: add and find.
/**
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
**/