Skip to content

Instantly share code, notes, and snippets.

@artlovan
artlovan / Partition.java
Created May 16, 2017 22:40
Partition (_2_4)
import java.util.ArrayList;
import java.util.List;
/**
* Partition: Write code to partition a linked list around a value x,
* such that all nodes less than x come before all nodes greater than
* or equal to x. lf x is contained within the list, the values of x only need
* to be after the elements less than x (see below).The partition element x can
* appear anywhere in the "right partition"; it does not need to appear between the left and right partitions.
* EXAMPLE
@artlovan
artlovan / DeleteMiddleNode.java
Created May 14, 2017 21:55
DeleteMiddleNode (_2_3)
/**
* Delete Middle Node: Implement an algorithm to delete a node in the middle
* (i.e., any node but the first and last node, not necessarily the exact middle)
* of a singly linked list, given only access to that node.
* EXAMPLE
* Input: the node c from the linked list a -> b -> c -> d -> e -> f
* Result: nothing is returned, but the new linked list looks like a -> b -> d -> e -> f
* Hints: #72
*/
public class DeleteMiddleNode {
@artlovan
artlovan / ReturnKthToLast.java
Created May 14, 2017 21:26
ReturnKthToLast (_2_2)
import java.util.ArrayList;
import java.util.List;
/**
* Return Kth to Last: Implement an algorithm to find the kth to last element of a singly linked list.
* Hints: #8, #25, #47, #67, # 726
*/
public class ReturnKthToLast {
public static void main(String[] args) {
Node<Integer> node1 = new Node<>(1, null);
@artlovan
artlovan / RemoveDups.java
Created May 14, 2017 20:08
RemoveDups (_2_1)
import java.util.LinkedList;
import java.util.List;
/**
* Remove Dups:
* Write code to remove duplicates from an unsorted linked list.
* <p>
* FOLLOW UP
* How would you solve this problem if a temporary buffer is not allowed?
@artlovan
artlovan / StringRotation.java
Created May 14, 2017 15:58
StringRotation (_1_9)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* String Rotation: Assume you have a method isSubstring which
* checks if one word is a substring of another.
* Given two strings, s1 and s2, write code to check if s2 is a rotation of s1
* using only one call to isSubstring
* (e.g.,"waterbottle" is a rotation of "erbottlewat").
@artlovan
artlovan / ZeroMatrix.java
Last active May 14, 2017 15:03
ZeroMatrix (_1_8)
import java.util.*;
/**
* Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0,
* its entire row and column are set to O.
* Hints: #17, #74, #102
*/
public class ZeroMatrix {
public static void main(String[] args) {
int[][] matrix = new int[][]{
@artlovan
artlovan / StringCompression.java
Created May 14, 2017 12:33
StringCompression (_1_6)
import java.util.HashMap;
import java.util.Map;
/**
* String Compression: Implement a method to perform basic string compression
* using the counts of repeated characters. For example,
* the string aabcccccaaa would become a2b1c5a3. If the "compressed"
* string would not become smaller than the original string, your method
* should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).
@artlovan
artlovan / URLify.java
Created May 13, 2017 19:54
URLify (_1_3_URLify)
/**
* URLify: Write a method to replace all spaces in a string with '%20:
* You may assume that the string has sufficient space at the end to hold
* the additional characters, and that you are given the "true" length of the string.
* (Note: If implementing in Java, please use a character array so that you can perform this operation in place.)
* EXAMPLE
* Input: "Mr John Smith " J 13 Output: "Mr%20John%20Smith" Hints: #53, #7 78
*/
public class URLify {
@artlovan
artlovan / IsUnique.java
Created May 13, 2017 18:40
CrackInterview._1_1_Is_Unique
import java.util.Arrays;
/**
Is Unique: Implement an algorithm to determine if a string has all unique characters.
What if you cannot use additional data structures?
Hints: #44, # 777, # 732
*/
public class IsUnique {
public static void main(String[] args) {
@artlovan
artlovan / FindPermutation.java
Last active May 13, 2017 19:32
find permutation in a string (_1_2_CheckPermutation)
import java.util.Arrays;
/**
Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
Hints: #7, #84, #722, #737
*/
public class CheckPermutation {
public static void main(String[] args) {
String pattern = "abbc";
String sequence = "cbabadcbbabbcbabaabccbabc";