├── pom.xml
└── src
├── main
├── java
│ ├── AnagramDetection.java
│ ├── ArrayConversion.java
│ ├── ArrayPairSumDetection.java
│ ├── BalancedBracketDetection.java
│ ├── BinarySearchTree.java
│ ├── BinarySearchTreeDetection.java
│ ├── BubbleSort.java
│ ├── Node.java
│ ├── PermutationGenerator.java
│ └── TelephoneWordsGenerator.java
└── resources
│ └── problems
│ ├── anagram-detection
│ └── Readme.md
│ ├── array-pair-sum
│ └── Readme.md
│ ├── balanced-brackets
│ └── Readme.md
│ ├── binary-search-tree-check
│ └── Readme.md
│ ├── binary-search-tree
│ └── Readme.md
│ ├── bubble-sort
│ └── Readme.md
│ ├── byte-format
│ └── README.md
│ ├── combine-two-strings
│ └── Readme.md
│ ├── convert-array
│ └── Readme.md
│ ├── csv-parsing
│ └── Readme.md
│ ├── debounce
│ └── Readme.md
│ ├── even-occuring-element
│ └── Readme.md
│ ├── factorial
│ └── readme.md
│ ├── fibonacci
│ └── Readme.md
│ ├── find-missing-element
│ └── Readme.md
│ ├── first-non-repeated-character
│ └── Readme.md
│ ├── flatten-array
│ └── Readme.md
│ ├── get-elements-by-class-name
│ └── Readme.md
│ ├── hotel-room
│ └── Readme.md
│ ├── insertion-sort
│ └── README.md
│ ├── integer-difference
│ └── Readme.md
│ ├── integer-length
│ └── Readme.md
│ ├── kth-largest-element-in-array
│ └── Readme.md
│ ├── largest-continuous-sum
│ └── Readme.md
│ ├── largest-palindrome
│ └── Readme.md
│ ├── linked-list
│ └── Readme.md
│ ├── longest-compound-word
│ └── Readme.md
│ ├── longest-words
│ └── Readme.md
│ ├── matching-nodes
│ └── Readme.md
│ ├── median-integer-stream
│ └── Readme.md
│ ├── merge-sort
│ └── Readme.md
│ ├── missing-number
│ └── Readme.md
│ ├── money-format
│ └── Readme.md
│ ├── multiples-of-3-and-5
│ └── Readme.md
│ ├── next-highest-number
│ └── Readme.md
│ ├── next-palindrome-number
│ └── Readme.md
│ ├── number-format
│ └── Readme.md
│ ├── odd-occuring-element
│ └── Readme.md
│ ├── once
│ └── Readme.md
│ ├── prime-number
│ └── Readme.md
│ ├── queue
│ └── Readme.md
│ ├── quick-sort
│ └── Readme.md
│ ├── remove-duplicates-from-string
│ └── Readme.md
│ ├── reverse-words-in-string
│ └── Readme.md
│ ├── search-unknown-length-array
│ └── Readme.md
│ ├── selection-sort
│ └── readme.md
│ ├── shortest-fizz-buzz
│ └── Readme.md
│ ├── sorted-array-search
│ └── Readme.md
│ ├── spiral
│ ├── Readme.md
│ ├── input-1.png
│ └── input-2.png
│ ├── stack-machine
│ └── Readme.md
│ ├── stack
│ └── Readme.md
│ ├── string-format
│ └── Readme.md
│ ├── string-permutations
│ └── Readme.md
│ ├── string-rotation
│ └── Readme.md
│ ├── string-transform
│ └── Readme.md
│ ├── sum-of-array-plus-one
│ └── Readme.md
│ ├── throttle
│ └── Readme.md
│ ├── transform-word
│ └── Readme.md
│ ├── tree-level-order-print
│ └── Readme.md
│ ├── word-analytics
│ ├── Readme.md
│ └── huckleberry-finn.txt
│ └── word-positions
│ └── Readme.md
└── test
└── java
├── AnagramDetectionTest.java
├── BalancedBracketDetectionTest.java
├── BinarySearchTreeDetectionTest.java
├── BinarySearchTreeTest.java
└── BubbleSortTest.java
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | code-problems
8 | code-problems
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | com.google.guava
14 | guava
15 | 16.0
16 |
17 |
18 | junit
19 | junit
20 | 4.11
21 |
22 |
23 | org.hamcrest
24 | hamcrest-all
25 | 1.3
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/src/main/java/AnagramDetection.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class AnagramDetection {
7 |
8 | int detector(String parent, String child) {
9 | ArrayList anagramList = Lists.newArrayList();
10 | generateAnagrams(anagramList, child);
11 | return occurenceCounter(anagramList, parent);
12 | }
13 |
14 | void generateAnagrams(ArrayList anagramList, String child) {
15 | char[] stringArray = child.toCharArray();
16 | heapPermute(stringArray.length, stringArray, anagramList);
17 | }
18 |
19 | void heapPermute(int n, char[] stringArray, List anagramList) {
20 | if(n==1){
21 | anagramList.add(new String(stringArray));
22 | }
23 | else{
24 | for(int i = 0; i < n; i ++){
25 | heapPermute(n - 1, stringArray, anagramList);
26 | if(n % 2 == 1)
27 | swap(0, n-1, stringArray);
28 | else
29 | swap(i, n-1, stringArray);
30 | }
31 | }
32 |
33 | }
34 |
35 | void swap(int i, int j, char[] stringArray){
36 | char temp = stringArray[i];
37 | stringArray[i] = stringArray[j];
38 | stringArray[j]= temp;
39 | }
40 |
41 |
42 | int occurenceCounter(ArrayList anagramList, String parent) {
43 | int counter = 0;
44 | for(String anagram : anagramList){
45 | if(parent.contains(anagram))
46 | counter++;
47 | }
48 | return counter;
49 | }
50 |
51 | public static void main(String[] args) {
52 | AnagramDetection anagramDetection = new AnagramDetection();
53 | String parent = "AdnBndAndBdaBn";
54 | String child = "And";
55 | int frequency = anagramDetection.detector(parent, child);
56 | System.out.println(frequency);
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/ArrayConversion.java:
--------------------------------------------------------------------------------
1 | import javax.xml.soap.SOAPPart;
2 |
3 | public class ArrayConversion {
4 |
5 | void convertArray(String[] array){
6 | int n = array.length/2;
7 | for(int i = 1 ; idesiredSum)
22 | j--;
23 | }
24 |
25 | }*/
26 |
27 | void findArrayPairSumForUnsortedArray(int desiredSum, int[] numberArray){
28 | int temp = 0, length = numberArray.length;
29 | System.out.print("[");
30 | for(int i =0; i< length; i++){
31 | temp = desiredSum - numberArray[i];
32 | for(int j = i + 1; j < length ; j++){
33 | if(numberArray[j] == temp )
34 | System.out.print(" [" +Integer.toString(numberArray[i]) + "," + Integer.toString(temp) + "] ");
35 | }
36 | }
37 | System.out.println("]");
38 | }
39 |
40 | public static void main(String[] args) {
41 | ArrayPairSumDetection arrayPairSumDetection = new ArrayPairSumDetection();
42 | arrayPairSumDetection.findArrayPairSumForUnsortedArray(10, new int[]{7, 4, 6, 5, 3});
43 | System.out.println();
44 | arrayPairSumDetection.findArrayPairSumForUnsortedArray(8, new int[]{4, 4, 5, 3, 4});
45 | }
46 |
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/BalancedBracketDetection.java:
--------------------------------------------------------------------------------
1 | import java.util.Stack;
2 |
3 | public class BalancedBracketDetection {
4 | public boolean checkBracketBalance(String bracketSequence){
5 | if(bracketSequence == null)
6 | return false;
7 |
8 | Stack bracketStack = new Stack();
9 | char[] bracketSequenceArray = bracketSequence.toCharArray();
10 | for(char element : bracketSequenceArray){
11 | if(bracketStack.isEmpty())
12 | bracketStack.push(element);
13 | else{
14 | char temp = bracketStack.peek();
15 | if(temp == "{".charAt(0) && element == "}".charAt(0))
16 | bracketStack.pop();
17 | else if(temp == "(".charAt(0) && element == ")".charAt(0))
18 | bracketStack.pop();
19 | else if(temp == "[".charAt(0) && element == "]".charAt(0))
20 | bracketStack.pop();
21 | else
22 | bracketStack.push(element);
23 | }
24 | }
25 | if(bracketStack.isEmpty())
26 | return true;
27 | else
28 | return false;
29 |
30 | }
31 |
32 |
33 | public static void main(String[] args) {
34 | BalancedBracketDetection balancedBracketDetection = new BalancedBracketDetection();
35 | Boolean balanceFlag;
36 |
37 | String firstBracketSequence = "()[]{}(([])){[()][]}";
38 | balanceFlag = balancedBracketDetection.checkBracketBalance(firstBracketSequence);
39 | System.out.println("Sequence " + firstBracketSequence +" is " + balanceFlag.toString());
40 |
41 | String secondBracketSequence = "())[]{}";
42 | balanceFlag = balancedBracketDetection.checkBracketBalance(secondBracketSequence);
43 | System.out.println("Sequence " + secondBracketSequence +" is " + balanceFlag.toString());
44 |
45 | String thirdBracketSequence = "[(])";
46 | balanceFlag = balancedBracketDetection.checkBracketBalance(thirdBracketSequence);
47 | System.out.println("Sequence " + thirdBracketSequence +" is " + balanceFlag.toString());
48 |
49 | }
50 | }
51 |
52 |
--------------------------------------------------------------------------------
/src/main/java/BinarySearchTree.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Queues;
2 |
3 | import java.util.ArrayList;
4 | import java.util.LinkedList;
5 | import java.util.Queue;
6 |
7 | public class BinarySearchTree {
8 | private Node root;
9 |
10 | public Node getRoot() {
11 | return root;
12 | }
13 |
14 | public void setRoot(Node root) {
15 | this.root = root;
16 | }
17 |
18 | public void setRoot(int rootValue) {
19 | this.root = new Node(rootValue);
20 | }
21 |
22 | public void insertValue(int newValue) {
23 | if (root == null)
24 | throw new NullPointerException("Insertion failed : Root node is null");
25 | Node tempNode = root;
26 | while (tempNode != null) {
27 | if (tempNode.getValue() >= newValue) {
28 | if (tempNode.getLeftChild() == null) {
29 | tempNode.setLeftChild(new Node(newValue));
30 | break;
31 | } else
32 | tempNode = tempNode.getLeftChild();
33 | } else if (tempNode.getValue() < newValue) {
34 | if (tempNode.getRightChild() == null) {
35 | tempNode.setRightChild(new Node(newValue));
36 | break;
37 | } else
38 | tempNode = tempNode.getRightChild();
39 | }
40 | }
41 | }
42 |
43 | public void recursiveInsert(Node element, int newValue) {
44 | if (newValue <= element.getValue()) {
45 | if (element.getLeftChild() == null)
46 | element.setLeftChild(new Node(newValue));
47 | else
48 | recursiveInsert(element.getLeftChild(), newValue);
49 | } else {
50 | if (element.getRightChild() == null)
51 | element.setRightChild(new Node(newValue));
52 | else
53 | recursiveInsert(element.getRightChild(), newValue);
54 | }
55 | }
56 |
57 | public boolean searchValue(Node element, int desiredValue) {
58 | if (desiredValue == element.getValue())
59 | return true;
60 | else if (desiredValue < element.getValue()) {
61 | return element.getLeftChild() != null && searchValue(element.getLeftChild(), desiredValue);
62 | } else if (element.getValue() < desiredValue) {
63 | return element.getRightChild() != null && searchValue(element.getRightChild(), desiredValue);
64 | }
65 | return false;
66 | }
67 |
68 | public void preOrderTreeTraversal(Node element, ArrayList traversalList) {
69 | traversalList.add(element.getValue());
70 | if (element.getLeftChild() != null)
71 | preOrderTreeTraversal(element.getLeftChild(), traversalList);
72 | if (element.getRightChild() != null)
73 | preOrderTreeTraversal(element.getRightChild(), traversalList);
74 | }
75 |
76 | public void inOrderTreeTraversal(Node element, ArrayList traversalList) {
77 | if (element.getLeftChild() != null)
78 | inOrderTreeTraversal(element.getLeftChild(), traversalList);
79 | traversalList.add(element.getValue());
80 | if (element.getRightChild() != null)
81 | inOrderTreeTraversal(element.getRightChild(), traversalList);
82 | }
83 |
84 | public void postOrderTreeTraversal(Node element, ArrayList traversalList) {
85 | if (element.getLeftChild() != null)
86 | postOrderTreeTraversal(element.getLeftChild(), traversalList);
87 | if (element.getRightChild() != null)
88 | postOrderTreeTraversal(element.getRightChild(), traversalList);
89 | traversalList.add(element.getValue());
90 | }
91 |
92 | public void breadthFirstTraversal(Node element, ArrayList traversalList){
93 | Queue nodeQueue = new LinkedList();
94 | nodeQueue.add(element);
95 | while (!nodeQueue.isEmpty()){
96 | Node firstElement = nodeQueue.remove();
97 | traversalList.add(firstElement.getValue());
98 | if(firstElement.getLeftChild() != null)
99 | nodeQueue.add(firstElement.getLeftChild());
100 | if(firstElement.getRightChild() != null)
101 | nodeQueue.add(firstElement.getRightChild());
102 | }
103 | }
104 |
105 | }
106 |
107 |
108 |
--------------------------------------------------------------------------------
/src/main/java/BinarySearchTreeDetection.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class BinarySearchTreeDetection {
6 |
7 | public boolean isBinarySearchTree(Node root){
8 | ArrayList traversedValues = Lists.newArrayList();
9 | traverseTreeInOrder(root, traversedValues);
10 | return isListSorted(traversedValues);
11 | }
12 |
13 | void traverseTreeInOrder(Node root, ArrayList traversedValues) {
14 | if(root.getLeftChild() != null)
15 | traverseTreeInOrder(root.getLeftChild(), traversedValues);
16 | traversedValues.add(root.getValue());
17 | if(root.getRightChild() != null)
18 | traverseTreeInOrder(root.getRightChild(), traversedValues);
19 | }
20 |
21 | boolean isListSorted(ArrayList traversedValues) {
22 | for(int i = 0; i traversedValues.get(i+1))
24 | return false;
25 | }
26 | return true;
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/BubbleSort.java:
--------------------------------------------------------------------------------
1 | import java.lang.reflect.Array;
2 | import java.util.ArrayList;
3 |
4 | public class BubbleSort {
5 | public void sortList(ArrayList unsortedList){
6 | int size = unsortedList.size();
7 | for(int i = 0; i unsortedList.get(j+1))
11 | swap(j, j+1, unsortedList);
12 | }
13 | }
14 | }
15 |
16 | void swap(int i, int j, ArrayList list){
17 | Integer temp = list.get(i);
18 | list.set(i, list.get(j));
19 | list.set(j, temp);
20 | }
21 |
22 | void displayList(ArrayList list){
23 | System.out.println();
24 | for(int i : list)
25 | System.out.print(Integer.toString(i) + " ");
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/Node.java:
--------------------------------------------------------------------------------
1 | public class Node {
2 | private int value;
3 | private Node leftChild;
4 | private Node rightChild;
5 |
6 | public Node(int value) {
7 | this.value = value;
8 | leftChild = null;
9 | rightChild = null;
10 | }
11 |
12 | public int getValue() {
13 | return value;
14 | }
15 |
16 | public void setValue(int value) {
17 | this.value = value;
18 | }
19 |
20 | public Node getLeftChild() {
21 | return leftChild;
22 | }
23 |
24 | public void setLeftChild(Node leftChild) {
25 | this.leftChild = leftChild;
26 | }
27 |
28 | public Node getRightChild() {
29 | return rightChild;
30 | }
31 |
32 | public void setRightChild(Node rightChild) {
33 | this.rightChild = rightChild;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/PermutationGenerator.java:
--------------------------------------------------------------------------------
1 | import sun.util.logging.resources.logging;
2 |
3 | import java.io.FileNotFoundException;
4 | import java.io.PrintWriter;
5 |
6 | public class PermutationGenerator {
7 | static PrintWriter printWriter;
8 |
9 | public void generate(String str) throws FileNotFoundException {
10 | printWriter = new PrintWriter("permuteLogs.txt");
11 | permute("", str);
12 | printWriter.close();
13 | }
14 |
15 | void permute(String prefix, String str) {
16 | int n = str.length();
17 | if(n == 0)
18 | System.out.println(prefix);
19 | else {
20 | logValues(prefix, str, n);
21 | for(int i = 0; i < n; i++)
22 | permute(prefix+str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
23 | }
24 | }
25 |
26 | void logValues(String prefix, String str, int n) {
27 | printWriter.println(prefix + "\t\t" + str + "\t\t" + n);
28 | printWriter.flush();
29 | }
30 |
31 | public static void main(String[] args) throws FileNotFoundException {
32 | PermutationGenerator generator = new PermutationGenerator();
33 | generator.generate("abcd");
34 | }
35 |
36 | }
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/TelephoneWordsGenerator.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 | import com.google.common.collect.Maps;
3 |
4 | import java.io.FileNotFoundException;
5 | import java.io.PrintWriter;
6 | import java.util.ArrayList;
7 | import java.util.Map;
8 |
9 | public class TelephoneWordsGenerator {
10 |
11 | static Map characterMap = Maps.newHashMap();
12 | static PrintWriter printWriter;
13 |
14 |
15 | public void generateWords(String number) throws FileNotFoundException {
16 | characterMap.put('0', "0");
17 | characterMap.put('1', "1");
18 | characterMap.put('2', "ABC");
19 | characterMap.put('3', "DEF");
20 | characterMap.put('4', "GHI");
21 | characterMap.put('5', "JKL");
22 | characterMap.put('6', "MNO");
23 | characterMap.put('7', "PRS");
24 | characterMap.put('8', "TUV");
25 | characterMap.put('9', "WXY");
26 |
27 | ArrayList words = Lists.newArrayList();
28 | printWriter = new PrintWriter("telephoneWords.txt");
29 | generate("", number, 0, words);
30 | printWriter.close();
31 | for(String word : words)
32 | System.out.println(word);
33 | }
34 |
35 | void generate(String prefix, String number, int index, ArrayList words){
36 | if(index == number.length()) {
37 | words.add(prefix);
38 | }
39 | else {
40 | logValues(prefix, number, index);
41 | for(char letter : characterMap.get(number.charAt(index)).toCharArray()){
42 | generate(prefix+letter, number, index+1, words);
43 | }
44 | }
45 | }
46 |
47 | void logValues(String prefix, String number, int index) {
48 | printWriter.println(prefix + "\t\t" + number + "\t\t" + index);
49 | printWriter.flush();
50 | }
51 |
52 | public static void main(String[] args) throws FileNotFoundException {
53 | TelephoneWordsGenerator generator = new TelephoneWordsGenerator();
54 | generator.generateWords("4971927");
55 | }
56 |
57 | }
58 |
59 |
--------------------------------------------------------------------------------
/src/main/resources/problems/anagram-detection/Readme.md:
--------------------------------------------------------------------------------
1 | # Anagram Detection
2 |
3 | Write a function that accepts two parameters, a parent and a child string. Determine how many times the child string - or an anagram of the of the child string - appears in the parent string. There is a solution which can be done in near instant time.
4 |
5 | ```js
6 | f('AdnBndAndBdaBn', 'dAn') // 4 ("Adn", "ndA", "dAn", "And")
7 | f('AbrAcadAbRa', 'cAda') // 2
8 | ```
9 |
--------------------------------------------------------------------------------
/src/main/resources/problems/array-pair-sum/Readme.md:
--------------------------------------------------------------------------------
1 | # Array Pair Sum
2 |
3 | Given an integer array, output all pairs that sum up to a specific value k. Consider the fact that the same number can add up to `k` with its duplicates in the array.
4 |
5 | > For example the array is [1, 1, 2, 3, 4] and the desired sum is 4. Should we output the pair (1, 3) twice or just once? Also do we output the reverse of a pair, meaning both (3, 1) and (1, 3)? Let’s keep the output as short as possible and print each pair only once. So, we will output only one copy of (1, 3). Also note that we shouldn’t output (2, 2) because it’s not a pair of two distinct elements.
6 |
7 | ## Example
8 |
9 | ```
10 | f(10, [3, 4, 5, 6, 7]) // [ [6, 4], [7, 3] ]
11 | f(8, [3, 4, 5, 4, 4]) // [ [3, 5], [4, 4], [4, 4], [4, 4] ]
12 | ```
13 |
14 | ## Source
15 |
16 | [http://www.ardendertat.com/2011/09/17/programming-interview-questions-1-array-pair-sum/](http://www.ardendertat.com/2011/09/17/programming-interview-questions-1-array-pair-sum/)
17 |
--------------------------------------------------------------------------------
/src/main/resources/problems/balanced-brackets/Readme.md:
--------------------------------------------------------------------------------
1 | # Balanced Brackets
2 |
3 | Write a function that accepts a string consisting entiring of brackets (`[](){}`) and returns whether it is balanced. Every "opening" bracket must be followed by a closing bracket of the same type. There can also be nested brackets, which adhere to the same rule.
4 |
5 | ```js
6 | f('()[]{}(([])){[()][]}') // true
7 | f('())[]{}') // false
8 | f('[(])') // false
9 | ```
10 |
--------------------------------------------------------------------------------
/src/main/resources/problems/binary-search-tree-check/Readme.md:
--------------------------------------------------------------------------------
1 | # Binary Search Tree Check
2 |
3 | Given a binary tree, check whether it’s a binary search tree or not.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/10/programming-interview-questions-7-binary-search-tree-check/](http://www.ardendertat.com/2011/10/10/programming-interview-questions-7-binary-search-tree-check/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/binary-search-tree/Readme.md:
--------------------------------------------------------------------------------
1 | # Binary Search Tree
2 |
3 | Gives methods to create a binary search tree.
4 |
5 | ## Source
6 |
7 | [http://en.wikipedia.org/wiki/Binary_search_tree](http://en.wikipedia.org/wiki/Binary_search_tree)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/bubble-sort/Readme.md:
--------------------------------------------------------------------------------
1 | # Bubble Sort
2 |
3 | Implement the [bubble sort algorithm](http://en.wikipedia.org/wiki/Bubble_sort).
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/byte-format/README.md:
--------------------------------------------------------------------------------
1 | # Byte String
2 |
3 | Convert a number to a string that represents a rounded size in bytes.
4 |
5 | ## Example
6 |
7 | ```
8 | f(156833213) // => "149.57 MB"
9 | f(8101) // => "7.91 KB"
10 | f(12331, 3) // => "12.042 KB"
11 | ```
12 |
13 | ## Source
14 |
15 | By [Riga](https://github.com/riga).
16 |
--------------------------------------------------------------------------------
/src/main/resources/problems/combine-two-strings/Readme.md:
--------------------------------------------------------------------------------
1 | # Combine Two Strings
2 |
3 | We are given 3 strings: str1, str2, and str3. Str3 is said to be a shuffle of str1 and str2 if it can be formed by interleaving the characters of str1 and str2 in a way that maintains the left to right ordering of the characters from each string. For example, given str1="abc" and str2="def", str3="dabecf" is a valid shuffle since it preserves the character ordering of the two strings. So, given these 3 strings write a function that detects whether str3 is a valid shuffle of str1 and str2.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/10/programming-interview-questions-6-combine-two-strings/](http://www.ardendertat.com/2011/10/10/programming-interview-questions-6-combine-two-strings/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/convert-array/Readme.md:
--------------------------------------------------------------------------------
1 | # Convert Array
2 |
3 | Given an array `[a1, a2, ..., aN, b1, b2, ..., bN, c1, c2, ..., cN]` convert it to `[a1, b1, c1, a2, b2, c2, ..., aN, bN, cN]` in-place using constant extra space.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/18/programming-interview-questions-9-convert-array/](http://www.ardendertat.com/2011/10/18/programming-interview-questions-9-convert-array/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/csv-parsing/Readme.md:
--------------------------------------------------------------------------------
1 | # CSV Parsing
2 |
3 | Write a function that accepts a string as it's only argument. The string consists of comma-separated values and all values are either an integer or a string. Return an array of the parsed input string.
4 |
5 | ```js
6 | f('2,6,3,2,5') // [ 2, 6, 3, 2, 5 ]
7 |
8 | f('"pears","apples","walnuts","grapes","cheese,cake"') // [ "pears", "apples", "walnuts", "grapes", "cheese,cake" ]
9 |
10 | f('1,"Que?","Kay?",2,"Si.","Sea? Kay, sea?","No, no, no. Que... ‘what’.",234,"Kay Watt?","Si, que ‘what’.","C.K. Watt?",3,"Yes!","comma,comma, comma , :)"') // [ 1, "Que?", "Kay?", 2, "Si.", "Sea? Kay, sea?", "No, no, no. Que... ‘what’." 234, "Kay Watt?", "Si, que ‘what’.", "C.K. Watt?", 3, "Yes!", "comma,comma, comma , :)" ]
11 | ```
12 |
--------------------------------------------------------------------------------
/src/main/resources/problems/debounce/Readme.md:
--------------------------------------------------------------------------------
1 | # Debounce
2 |
3 | Write a function that accepts a function and timeout, `x`, in number of milliseconds. It will return a new function that can only be executed on per timeout period - and if the function is invoked during the timeout period, the timeout period restarts. This is useful for functions that can be need to be blocked on subsequent attempts over short period of times. Once such is example, is clicks on a button.
4 |
5 | Once written, add a third parameter that will allow the function to be executed immediately if set to true. Otherwise the function will run at the end of the timeout period.
6 |
--------------------------------------------------------------------------------
/src/main/resources/problems/even-occuring-element/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Even Occurring Element
2 |
3 | Given an integer array, one element occurs even number of times and all others have odd occurrences. Find the element with even occurrences.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/11/29/programming-interview-questions-18-find-even-occurring-element/](http://www.ardendertat.com/2011/11/29/programming-interview-questions-18-find-even-occurring-element/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/factorial/readme.md:
--------------------------------------------------------------------------------
1 | Factorial
2 | ==========
3 |
4 | Factorial of any number n is defined as the multiplication of numbers from one to the given number.
5 |
6 | ``
7 | n! = 1 x 2 x 3 x 4 x ........ x n
8 | ``
9 |
--------------------------------------------------------------------------------
/src/main/resources/problems/fibonacci/Readme.md:
--------------------------------------------------------------------------------
1 | # Fibonacci
2 |
3 | By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two.
4 |
5 | For example, the first ten Fibonacci numbers are:
6 |
7 | ```
8 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
9 | ```
10 |
11 | Write a function that accepts a number and returns the number at that position in the fibonnaci sequence.
12 |
--------------------------------------------------------------------------------
/src/main/resources/problems/find-missing-element/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Missing Element
2 |
3 | There is an array of non-negative integers. A second array is formed by shuffling the elements of the first array and deleting a random element. Given these two arrays, find which element is missing in the second array.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/09/27/programming-interview-questions-4-find-missing-element/](http://www.ardendertat.com/2011/09/27/programming-interview-questions-4-find-missing-element/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/first-non-repeated-character/Readme.md:
--------------------------------------------------------------------------------
1 | # First Non-repeated character
2 |
3 | Write a function that accepts a single string input and returns the first non-repeated character.
4 |
5 | ```js
6 | "AABBC" // "C"
7 | "AABBCCDEEFF" // "D"
8 | ```
9 |
--------------------------------------------------------------------------------
/src/main/resources/problems/flatten-array/Readme.md:
--------------------------------------------------------------------------------
1 | # Flatten Array
2 |
3 | Write a function that accepts a multi dimensional array and returns a flattened version.
4 |
5 | ```javascript
6 | flatten([1, 2, [3, [4], 5, 6], 7]) // [1, 2, 3, 4, 5, 6, 7]
7 | ```
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/get-elements-by-class-name/Readme.md:
--------------------------------------------------------------------------------
1 | # Get Elements by Class Name
2 |
3 | Implement the `getElementsByClassName(element, className)` function in Javascript.
4 |
5 | ## Source
6 |
7 | [GlassDoor](http://www.glassdoor.com/Interview/Implement-the-getElementsByClassName-element-className-function-in-Javascript-QTN_226449.htm)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/hotel-room/Readme.md:
--------------------------------------------------------------------------------
1 | # Room Number
2 |
3 | You're in a hotel and you forgot what room number you were in but remember that the sum of it's divisors is greater than the number, yet there is no subset of those divisors that add up to the number itself. There are 100 rooms in the Hotel, what's your room number?
4 |
5 | ## Source
6 |
7 | [Reddit](http://www.reddit.com/r/javascript/comments/1ftyjh/common_code_problems_solved_in_javascript_xpost/cae25ra)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/insertion-sort/README.md:
--------------------------------------------------------------------------------
1 | # Insertion Sort
2 |
3 | Implement the [insertion sort algorithm](http://en.wikipedia.org/wiki/Insertion_sort).
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/integer-difference/Readme.md:
--------------------------------------------------------------------------------
1 | # Integer Difference
2 |
3 | Write a function that accepts an array of random integers and an integer *n*. Determine the number of times where two integers in the array have the difference of *n*.
4 |
5 | ```js
6 | f(4, [1, 1, 5, 6, 9, 16, 27]) // 3 (Due to 2x [1, 5], and [5, 9])
7 | f(2, [1, 1, 3, 3]) // 4 (Due to 4x [1, 3])
8 | ```
9 |
--------------------------------------------------------------------------------
/src/main/resources/problems/integer-length/Readme.md:
--------------------------------------------------------------------------------
1 | # Integer Length
2 |
3 | Write a function that takes an integer as input and returns the number of digits in that integer.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/kth-largest-element-in-array/Readme.md:
--------------------------------------------------------------------------------
1 | # Kth Largest Element in Array
2 |
3 | Given an array of integers find the kth element in the sorted order (not the kth distinct element). So, if the array is `[3, 1, 2, 1, 4]` and k is 3 then the result is 2, because it’s the 3rd element in sorted order (but the 3rd distinct element is 3).
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/27/programming-interview-questions-10-kth-largest-element-in-array/](http://www.ardendertat.com/2011/10/27/programming-interview-questions-10-kth-largest-element-in-array/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/largest-continuous-sum/Readme.md:
--------------------------------------------------------------------------------
1 | # Largest Continuous Sum
2 |
3 | Given an array of integers (positive and negative) find the largest continuous sum.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/09/24/programming-interview-questions-3-largest-continuous-sum/](http://www.ardendertat.com/2011/09/24/programming-interview-questions-3-largest-continuous-sum/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/largest-palindrome/Readme.md:
--------------------------------------------------------------------------------
1 | # Largest Palindrome
2 |
3 | Write a function that finds the largest palindrome in a string. All characters can be valid for the palindrome, including whitespace. In the string "I am a red racecar driver" - the largest palindrome would be "d racecar d".
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/linked-list/Readme.md:
--------------------------------------------------------------------------------
1 | # Linked List
2 |
3 | Write a linked list implementation, better yet - make it doubly linked.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/longest-compound-word/Readme.md:
--------------------------------------------------------------------------------
1 | # Longest Compound Word
2 |
3 | Given a sorted list of words, find the longest compound word in the list that is constructed by concatenating the words in the list. For example, if the input list is: `['cat', 'cats', 'catsdogcats', 'catxdogcatsrat', 'dog', 'dogcatsdog', 'hippopotamuses', 'rat', 'ratcat', 'ratcatdog', 'ratcatdogcat']`. Then the longest compound word is ‘ratcatdogcat’ with 12 letters. Note that the longest individual words are ‘catxdogcatsrat’ and ‘hippopotamuses’ with 14 letters, but they’re not fully constructed by other words. Former one has an extra ‘x’ letter, and latter is an individual word by itself not a compound word.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2012/06/15/programming-interview-questions-28-longest-compound-word/](http://www.ardendertat.com/2012/06/15/programming-interview-questions-28-longest-compound-word/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/longest-words/Readme.md:
--------------------------------------------------------------------------------
1 | # Longest Words
2 |
3 | Write a function that returns the longest word(s) from a sentence. The function should not return any duplicate words (case-insensitive).
4 |
5 | ## Example
6 |
7 | ```js
8 | longestWords("You are just an old antidisestablishmentarian") // ["antidisestablishmentarian"]
9 | longestWords("I gave a present to my parents") // ["present", "parents"]
10 | longestWords("Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo") // ["buffalo"] or ["Buffalo"]
11 | ```
12 |
--------------------------------------------------------------------------------
/src/main/resources/problems/matching-nodes/Readme.md:
--------------------------------------------------------------------------------
1 | # Matching Nodes
2 |
3 | Given two identical tree structures (consider them DOM tree if you want) and a node from the first tree, find and return the same node on the second tree. Assume you have access to a function that can test whether two nodes are the same (E.g. the node in the first tree matches the one in the second tree). Consider ways to optimize your approach.
4 |
5 | ## Source
6 |
7 | In-person interview question using whiteboard
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/median-integer-stream/Readme.md:
--------------------------------------------------------------------------------
1 | # Median of Integer Stream
2 |
3 | Given a stream of unsorted integers, find the median element in sorted order at any given time. So, we will be receiving a continuous stream of numbers in some random order and we don’t know the stream length in advance. Write a function that finds the median of the already received numbers efficiently at any time. We will be asked to find the median multiple times. Just to recall, median is the middle element in an odd length sorted array, and in the even case it’s the average of the middle elements.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/11/03/programming-interview-questions-13-median-of-integer-stream/](http://www.ardendertat.com/2011/11/03/programming-interview-questions-13-median-of-integer-stream/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/merge-sort/Readme.md:
--------------------------------------------------------------------------------
1 | # Merge Sort
2 |
3 | Implement the [merge sort algorithm](http://en.wikipedia.org/wiki/Merge_sort).
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/missing-number/Readme.md:
--------------------------------------------------------------------------------
1 | # Missing Number
2 |
3 | Write a function that accepts an array of integers in random order of unknown length, but with one number missing. Return the missing number.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/money-format/Readme.md:
--------------------------------------------------------------------------------
1 | # Money Formatting
2 |
3 | Given an amount of money as a float, format it as a string.
4 |
5 | ```javascript
6 | formatMoney(2310000.159897); // '2 310 000.16'
7 | formatMoney(1600); // '1 600.00'
8 | ```
9 |
--------------------------------------------------------------------------------
/src/main/resources/problems/multiples-of-3-and-5/Readme.md:
--------------------------------------------------------------------------------
1 | # Multiples of 3 and 5
2 |
3 | If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
4 |
5 | Find the sum of all the multiples of 3 or 5 below 1000.
6 |
7 | ## Source
8 |
9 | [http://projecteuler.net/problem=1](http://projecteuler.net/problem=1)
10 |
--------------------------------------------------------------------------------
/src/main/resources/problems/next-highest-number/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Next Higher Number With Same Digits
2 |
3 | Given a number, find the next higher number using only the digits in the given number. For example if the given number is 1234, next higher number with same digits is 1243.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/](http://www.ardendertat.com/2012/01/02/programming-interview-questions-24-find-next-higher-number-with-same-digits/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/next-palindrome-number/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Next Palindrome Number
2 |
3 | Given a number, find the next smallest palindrome larger than the number. For example if the number is 125, next smallest palindrome is 131.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/12/01/programming-interview-questions-19-find-next-palindrome-number/](http://www.ardendertat.com/2011/12/01/programming-interview-questions-19-find-next-palindrome-number/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/number-format/Readme.md:
--------------------------------------------------------------------------------
1 | # Numeric String
2 |
3 | Format any number into a string with "," (commas) in the correct places. E.g. "1,000,000".
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/odd-occuring-element/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Odd Occurring Element
2 |
3 | Given an integer array, one element occurs odd number of times and all others have even occurrences. Find the element with odd occurrences.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/12/13/programming-interview-questions-22-find-odd-occurring-element/](http://www.ardendertat.com/2011/12/13/programming-interview-questions-22-find-odd-occurring-element/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/once/Readme.md:
--------------------------------------------------------------------------------
1 | # Once
2 |
3 | Write a function that accepts a function as it's only argument and returns a new function that can only ever be executed once.
4 |
5 | Once completed, add a second arguments that allows the function to be executed `x` number of times before it stops working.
6 |
--------------------------------------------------------------------------------
/src/main/resources/problems/prime-number/Readme.md:
--------------------------------------------------------------------------------
1 | # Prime Number
2 |
3 | Write a function that accepts a number and return a boolean based on whether it's a prime number.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/queue/Readme.md:
--------------------------------------------------------------------------------
1 | # Basic Queue
2 |
3 | Implement a basic queue function with the ability to `add` and `remove` values.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/quick-sort/Readme.md:
--------------------------------------------------------------------------------
1 | # Quick Sort
2 |
3 | Implement the [quick sort algorithm](http://en.wikipedia.org/wiki/Quicksort).
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/remove-duplicates-from-string/Readme.md:
--------------------------------------------------------------------------------
1 | # Remove Duplicate Characters in String
2 |
3 | Remove duplicate characters in a given string keeping only the first occurrences. For example, if the input is ‘tree traversal’ the output will be "tre avsl".
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2012/01/06/programming-interview-questions-25-remove-duplicate-characters-in-string/](http://www.ardendertat.com/2012/01/06/programming-interview-questions-25-remove-duplicate-characters-in-string/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/reverse-words-in-string/Readme.md:
--------------------------------------------------------------------------------
1 | # Reverse Words in a String
2 |
3 | Given an input string, reverse all the words. To clarify, input: "Interviews are awesome!" output: "awesome! are Interviews". Consider all consecutive non-whitespace characters as individual words. If there are multiple spaces between words reduce them to a single white space. Also remove all leading and trailing whitespaces. So, the output for " CS degree", "CS degree", "CS degree ", or " CS degree " are all the same: "degree CS".
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/31/programming-interview-questions-12-reverse-words-in-a-string/](http://www.ardendertat.com/2011/10/31/programming-interview-questions-12-reverse-words-in-a-string/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/search-unknown-length-array/Readme.md:
--------------------------------------------------------------------------------
1 | # Search Unknown Length Array
2 |
3 | Given a sorted array of unknown length and a number to search for, return the index of the number in the array. Accessing an element out of bounds throws exception. If the number occurs multiple times, return the index of any occurrence. If it isn’t present, return -1.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/selection-sort/readme.md:
--------------------------------------------------------------------------------
1 | # Selection Sort
2 |
3 | Implement the [selection sort algorithm](http://en.wikipedia.org/wiki/Selection_sort).
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/shortest-fizz-buzz/Readme.md:
--------------------------------------------------------------------------------
1 | # Shortest Fizz Buzz
2 |
3 | Write a program that prints (to STDOUT) the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
4 |
5 | The goal is to write the shortest code possible.
6 |
7 | Expected output: [http://cdn.hackerrank.com/fizzbuzz.txt](http://cdn.hackerrank.com/fizzbuzz.txt)
8 |
9 | Your output should exactly match the above.
10 |
11 | ## Source
12 |
13 | [https://www.hackerrank.com/challenges/fizzbuzz](https://www.hackerrank.com/challenges/fizzbuzz)
14 |
--------------------------------------------------------------------------------
/src/main/resources/problems/sorted-array-search/Readme.md:
--------------------------------------------------------------------------------
1 | # Sorted Array Search
2 |
3 | Given an array of numbers sorted in ascending order, write a function that will return the index at which the number is found.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/spiral/Readme.md:
--------------------------------------------------------------------------------
1 | # Spiral
2 |
3 | Write a function that accepts four arguments. The first two arguments are the size of the grid (*h x w*), filled with ascending integers from left to right, top to bottom, starting from 1. The next two arguments are the starting positions, the row (*r*) and column (*c*).
4 |
5 | Return an array of integers obtained by spiralling outward anti-clockwise from the *r* and *c*, starting upward.
6 |
7 | ```
8 | f(5, 5, 3, 3) // [ 13, 8, 7, 12, 17, 18, 19, 14, 9, 4, 3, 2, 1, 6, 11, 16, 21, 22, 23, 24, 25, 20, 15, 10, 5 ]
9 |
10 | f(2, 4, 1, 2) // [ 2, 1, 5, 6, 7, 3, 8, 4 ]
11 | ```
12 |
13 | **Supporting Content**
14 |
15 | The following graphics show the grid in question and the spiral generated, beginning at cell (3, 3).
16 |
17 | 
18 |
19 | 
20 |
--------------------------------------------------------------------------------
/src/main/resources/problems/spiral/input-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thezelus/code-problems/7000045c74f423a310e1c8b3a847fcb02f25cec9/src/main/resources/problems/spiral/input-1.png
--------------------------------------------------------------------------------
/src/main/resources/problems/spiral/input-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/thezelus/code-problems/7000045c74f423a310e1c8b3a847fcb02f25cec9/src/main/resources/problems/spiral/input-2.png
--------------------------------------------------------------------------------
/src/main/resources/problems/stack-machine/Readme.md:
--------------------------------------------------------------------------------
1 | # Stack Machine
2 |
3 | A stack machine is a simple system that performs arithmetic operations on an input string of numbers and operators. It contains a stack that can store an arbitrary number of 12-bit unsigned integers. Initially the stack is empty. The machine processes a string of characters in the following way:
4 |
5 | 1. the characters of the string are processed one by one;
6 | 2. if the current character is a digit ('0'-'9'), the machine pushes the value of that digit onto its stack;
7 | 3. if the current character is '+', the machine pops the two topmost values from its stack, adds them and pushes the result onto the stack;
8 | 4. if the current character is '*', the machine pops the two topmost values from its stack, multiplies them and pushes the result onto the stack;
9 | 5. after the machine has processed the whole string it returns the topmost value of its stack as the result;
10 | 6. the machine reports an error if any operation it performs (addition or multiplication) results in an overflow;
11 | 7. the machine reports an error if it tries to pop an element from its stack when the stack is empty, or if the stack is empty after the machine has processed the whole string.
12 |
13 | For example, given the string "13+62*7+*" the machine will perform the following operations:
14 |
15 | ```
16 | character | comment | stack
17 | -----------------------------------------------
18 | | | [empty]
19 | '1' | push 1 onto the stack |
20 | | | 1
21 | '3' | push 3 onto the stack |
22 | | | 1, 3
23 | '+' | perform addition |
24 | | | 4
25 | '6' | push 6 onto the stack |
26 | | | 4, 6
27 | '2' | push 2 onto the stack |
28 | | | 4, 6, 2
29 | '*' | perform multiplication |
30 | | | 4, 12
31 | '7' | push 7 onto the stack |
32 | | | 4, 12, 7
33 | '+' | perform addition |
34 | | | 4, 19
35 | '*' | perform multiplication |
36 | | | 76
37 | ```
38 |
39 | The machine will return 76 as the result as it is the topmost element of its stack.
40 |
41 | Write a function:
42 |
43 | ```
44 | function solution(S);
45 | ```
46 |
47 | that, given a string S consisting of N characters containing input for the stack machine, returns the result the machine would return if given this string. The function should return −1 if the machine would report an error when processing the string.
48 |
49 | For example, given string `S = "13+62*7+*"` the function should return `76`, as explained in the example above. Given string `S = "11++"` the function should return `−1`.
50 |
51 | Assume that:
52 | * the length of S is within the range `[0..1,000,000]`;
53 | * string S consists only of the following characters: `"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+" and/or "*"`.
54 |
55 | Complexity:
56 | * expected worst-case time complexity is O(N);
57 | * expected worst-case space complexity is O(N) (not counting the storage required for input arguments).
58 |
--------------------------------------------------------------------------------
/src/main/resources/problems/stack/Readme.md:
--------------------------------------------------------------------------------
1 | # Basic Stack
2 |
3 | Implement a basic stack function with the ability to `add` and `remove` values.
4 |
--------------------------------------------------------------------------------
/src/main/resources/problems/string-format/Readme.md:
--------------------------------------------------------------------------------
1 | # String Format
2 |
3 | Create a string formatting function that accepts an input string and a number of arguments to replace positions in the input string.
4 |
5 | ## Example
6 |
7 | ```
8 | f('Hello {0} {1}', 'Mr.', 'X') // => 'Hello Mr. X'
9 | f('{1}_{0}', '{1}', '{0}') // => '{0}_{1}'
10 | ```
11 |
12 | By [Riga](https://github.com/riga).
13 |
--------------------------------------------------------------------------------
/src/main/resources/problems/string-permutations/Readme.md:
--------------------------------------------------------------------------------
1 | # All Permutations of a String
2 |
3 | Generate all permutations of a given string. (Note: also known as the generating anagrams problem).
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/10/28/programming-interview-questions-11-all-permutations-of-string/](http://www.ardendertat.com/2011/10/28/programming-interview-questions-11-all-permutations-of-string/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/string-rotation/Readme.md:
--------------------------------------------------------------------------------
1 | # String Rotation
2 |
3 | Find out if a string is a rotation of another string. E.g. `ABCD` is a rotation of `BCDA` but not `ACBD`.
4 |
5 | ## Source
6 |
7 | [Reddit](http://www.reddit.com/r/javascript/comments/1ftyjh/common_code_problems_solved_in_javascript_xpost/cae25ra)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/string-transform/Readme.md:
--------------------------------------------------------------------------------
1 | A string S consisting only of letters "A", "B" and "C" is given. This string can be transformed according to one of the following rules:
2 |
3 | 1. substitute some occurrence of "AB" with "AA",
4 | 2. substitute some occurrence of "BA" with "AA",
5 | 3. substitute some occurrence of "CB" with "CC",
6 | 4. substitute some occurrence of "BC" with "CC",
7 | 5. substitute some occurrence of "AA" with "A",
8 | 6. substitute some occurrence of "CC" with "C".
9 |
10 | As long as it is possible to transform the string, a rule is picked at random and the string is transformed according to that rule.
11 |
12 | Write a function:
13 |
14 | ```
15 | function solution(S);
16 | ```
17 |
18 | that, given a string S consisting of N characters, returns any string that can result from a sequence of transformations as described above.
19 |
20 | For example, given string S = "AABBCC" the function may return "AC" because this is one of the possible sequences of transformations:
21 |
22 | * "AABBCC" becomes "AAABCC" (rule 1);
23 | * "AAABCC" becomes "AABCC" (rule 5);
24 | * "AABCC" becomes "AACCC" (rule 4);
25 | * "AACCC" becomes "AACC" (rule 6);
26 | * "AACC" becomes "ACC" (rule 5);
27 | * "ACC" becomes "AC" (rule 5);
28 | * no further rule can be applied.
29 |
30 | Assume that:
31 | * the length of S is within the range [0..100,000];
32 | * string S consists only of the following characters: "A", "B" and/or "C".
33 |
34 | Complexity:
35 | * expected worst-case time complexity is O(N);
36 | * expected worst-case space complexity is O(N) (not counting the storage required for input arguments).
37 |
--------------------------------------------------------------------------------
/src/main/resources/problems/sum-of-array-plus-one/Readme.md:
--------------------------------------------------------------------------------
1 | # Sum of Array Plus One
2 |
3 | Write a function that takes an array of integers and returns the sum of the integers after adding 1 to each.
4 |
5 | ```
6 | plusOneSum([1, 2, 3, 4]); // 14
7 | ```
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/throttle/Readme.md:
--------------------------------------------------------------------------------
1 | # Throttle
2 |
3 | Write a function that accepts a function and timeout, `x`, in number of milliseconds. It returns a function that can only be executed once per `x` milliseconds. This can be useful for limiting the number of time and computation heavy function that are run. For example, making AJAX requests to an autocompletion API.
4 |
5 | Once written, add a third parameter that will allow the function to be executed immediately if set to true. Otherwise the function will run at the end of the timeout period.
6 |
--------------------------------------------------------------------------------
/src/main/resources/problems/transform-word/Readme.md:
--------------------------------------------------------------------------------
1 | # Transform Word
2 |
3 | Given a source word, target word and an English dictionary, transform the source word to target by changing/adding/removing 1 character at a time, while all intermediate words being valid English words. Return the transformation chain which has the smallest number of intermediate words.
4 |
5 | ## Example
6 |
7 | ```js
8 | transformWord(['cat', 'bat', 'bet', 'bed', 'at', 'ad', 'ed'], 'cat', 'bed');
9 | ```
10 |
11 | ## Source
12 |
13 | [http://www.ardendertat.com/2011/10/17/programming-interview-questions-8-transform-word/](http://www.ardendertat.com/2011/10/17/programming-interview-questions-8-transform-word/)
14 |
--------------------------------------------------------------------------------
/src/main/resources/problems/tree-level-order-print/Readme.md:
--------------------------------------------------------------------------------
1 | # Tree Level Order Print
2 |
3 | Given a binary tree of integers, print it in level order. The output will contain space between the numbers in the same level, and new line between different levels.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/12/05/programming-interview-questions-20-tree-level-order-print/](http://www.ardendertat.com/2011/12/05/programming-interview-questions-20-tree-level-order-print/)
8 |
--------------------------------------------------------------------------------
/src/main/resources/problems/word-analytics/Readme.md:
--------------------------------------------------------------------------------
1 | # Word Analytics
2 |
3 | You're a newly hired engineer for a brand-new company that's building a "killer Word-like application". You've been specifically assigned to implement a tool that gives the user some details on common word usage, letter usage, and some other analytics for a given document! More specifically, you must read a given text file (no special formatting, just a plain ASCII text file) and print off the following details:
4 |
5 | 1. Number of words
6 | 2. Number of letters
7 | 3. Number of symbols (any non-letter and non-digit character, excluding white spaces)
8 | 4. Top three most common words (you may count "small words", such as "it" or "the")
9 | 5. Top three most common letters
10 | 6. Most common first word of a paragraph (paragraph being defined as a block of text with an empty line above it) (Optional bonus)
11 | 7. Number of words only used once (Optional bonus)
12 | 8. All letters not used in the document (Optional bonus)
13 |
14 | Please note that your tool does not have to be case sensitive, meaning the word "Hello" is the same as "hello" and "HELLO".
15 |
16 | *Author: nint22*
17 |
18 | ## Formal Inputs & Outputs
19 |
20 | ### Input Description
21 |
22 | As an argument to your program on the command line, you will be given a text file location (such as "C:\Users\nint22\Document.txt" on Windows or "/Users/nint22/Document.txt" on any other sane file system). This file may be empty, but will be guaranteed well-formed (all valid ASCII characters). You can assume that line endings will follow the UNIX-style new-line ending (unlike the Windows carriage-return & new-line format ).
23 |
24 | ### Output Description
25 |
26 | For each analytic feature, you must print the results in a special string format. Simply you will print off 6 to 8 sentences with the following format:
27 |
28 | ```
29 | "A words", where A is the number of words in the given document
30 | "B letters", where B is the number of letters in the given document
31 | "C symbols", where C is the number of non-letter and non-digit character, excluding white spaces, in the document
32 | "Top three most common words: D, E, F", where D, E, and F are the top three most common words
33 | "Top three most common letters: G, H, I", where G, H, and I are the top three most common letters
34 | "J is the most common first word of all paragraphs", where J is the most common word at the start of all paragraphs in the document (paragraph being defined as a block of text with an empty line above it) (*Optional bonus*)
35 | "Words only used once: K", where K is a comma-delimited list of all words only used once (*Optional bonus*)
36 | "Letters not used in the document: L", where L is a comma-delimited list of all alphabetic characters not in the document (*Optional bonus*)
37 | ```
38 |
39 | If there are certain lines that have no answers (such as the situation in which a given document has no paragraph structures), simply do not print that line of text. In this example, I've just generated some random Lorem Ipsum text.
40 |
41 | ## Testing
42 |
43 | ```
44 | cat huckleberry-finn.txt | node index.js
45 | ```
46 |
47 | ## Source
48 |
49 | [http://www.reddit.com/r/dailyprogrammer/comments/1e97ob/051313_challenge_125_easy_word_analytics/](http://www.reddit.com/r/dailyprogrammer/comments/1e97ob/051313_challenge_125_easy_word_analytics/)
50 |
--------------------------------------------------------------------------------
/src/main/resources/problems/word-positions/Readme.md:
--------------------------------------------------------------------------------
1 | # Find Word Positions in Text
2 |
3 | Given a text file and a word, find the positions that the word occurs in the file. We’ll be asked to find the positions of many words in the same file.
4 |
5 | ## Source
6 |
7 | [http://www.ardendertat.com/2011/12/20/programming-interview-questions-23-find-word-positions-in-text/](http://www.ardendertat.com/2011/12/20/programming-interview-questions-23-find-word-positions-in-text/)
8 |
--------------------------------------------------------------------------------
/src/test/java/AnagramDetectionTest.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 | import org.junit.Test;
3 |
4 | import java.util.ArrayList;
5 |
6 | import static org.hamcrest.MatcherAssert.assertThat;
7 |
8 | public class AnagramDetectionTest {
9 | @Test
10 | public void testHeapPermute() throws Exception {
11 | AnagramDetection anagramDetection = new AnagramDetection();
12 | String testString = "ABCD";
13 | ArrayList testAnagramList = Lists.newArrayList();
14 | anagramDetection.heapPermute(testString.length(), testString.toCharArray(), testAnagramList);
15 | assert(testAnagramList.size() == 24);
16 | assert(testAnagramList.contains("ABCD"));
17 | assert(testAnagramList.contains("DCBA"));
18 | assert(testAnagramList.contains("CDAB"));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/test/java/BalancedBracketDetectionTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.Test;
2 |
3 | import static org.hamcrest.MatcherAssert.assertThat;
4 | import static org.junit.Assert.assertFalse;
5 | import static org.junit.Assert.assertTrue;
6 |
7 | public class BalancedBracketDetectionTest {
8 | @Test
9 | public void testCheckBracketBalance() throws Exception {
10 |
11 | BalancedBracketDetection testBalancedBracketDetection = new BalancedBracketDetection();
12 | assertTrue(testBalancedBracketDetection.checkBracketBalance("()[]{}(([])){[()][]}"));
13 | assertFalse(testBalancedBracketDetection.checkBracketBalance("("));
14 | assertFalse(testBalancedBracketDetection.checkBracketBalance(")"));
15 | assertFalse(testBalancedBracketDetection.checkBracketBalance(")("));
16 | assertTrue(testBalancedBracketDetection.checkBracketBalance(""));
17 | assertFalse(testBalancedBracketDetection.checkBracketBalance(null));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/test/java/BinarySearchTreeDetectionTest.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 | import org.junit.Test;
3 |
4 | import java.util.ArrayList;
5 |
6 | import static org.hamcrest.MatcherAssert.assertThat;
7 | import static org.hamcrest.Matchers.equalTo;
8 |
9 | public class BinarySearchTreeDetectionTest {
10 | @Test
11 | public void testIsBinarySearchTree_withSimpleBinaryTree() throws Exception {
12 | Node root = new Node(5);
13 | root.setRightChild(new Node(2));
14 | root.setLeftChild(new Node(8));
15 |
16 | BinarySearchTreeDetection binarySearchTreeDetection = new BinarySearchTreeDetection();
17 | assertThat(binarySearchTreeDetection.isBinarySearchTree(root), equalTo(false));
18 | }
19 |
20 | @Test
21 | public void testIsBinarySearchTree_withBinarySearchTree() throws Exception {
22 | BinarySearchTree binarySearchTree = new BinarySearchTree();
23 | binarySearchTree.setRoot(new Node(50));
24 | int[] testArray = new int[]{17, 1, 11, 65, 99, 0, 12, 4, 18, 2, 121};
25 | Node root = binarySearchTree.getRoot();
26 | for(int temp : testArray)
27 | binarySearchTree.recursiveInsert(root, temp);
28 |
29 | BinarySearchTreeDetection detection = new BinarySearchTreeDetection();
30 |
31 | assertThat(detection.isBinarySearchTree(root), equalTo(true));
32 | }
33 |
34 | @Test
35 | public void testIsListSorted() throws Exception {
36 | BinarySearchTreeDetection binarySearchTreeDetection = new BinarySearchTreeDetection();
37 |
38 | ArrayList sortedList = Lists.newArrayList(0,1,2,3,4,5,6,7);
39 | ArrayList unsortedList = Lists.newArrayList(2,1,5,6,7,4,3);
40 |
41 | assertThat(binarySearchTreeDetection.isListSorted(sortedList), equalTo(true));
42 | assertThat(binarySearchTreeDetection.isListSorted(unsortedList), equalTo(false));
43 |
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/BinarySearchTreeTest.java:
--------------------------------------------------------------------------------
1 | import com.google.common.collect.Lists;
2 | import org.hamcrest.Matcher;
3 | import org.junit.Test;
4 |
5 | import java.util.ArrayList;
6 |
7 | import static junit.framework.Assert.assertFalse;
8 | import static junit.framework.Assert.assertTrue;
9 | import static org.hamcrest.CoreMatchers.equalTo;
10 | import static org.hamcrest.CoreMatchers.hasItems;
11 | import static org.hamcrest.MatcherAssert.assertThat;
12 |
13 | public class BinarySearchTreeTest {
14 |
15 | public static BinarySearchTree getTestTree(int[] testValueArray, int testRoot) throws Exception {
16 | BinarySearchTree testBinarySearchTree = new BinarySearchTree();
17 |
18 | testBinarySearchTree.setRoot(testRoot);
19 | for(int temp : testValueArray){
20 | testBinarySearchTree.insertValue(temp);
21 | }
22 |
23 | return testBinarySearchTree;
24 | }
25 |
26 | @Test
27 | public void testInsertValue() throws Exception {
28 | BinarySearchTree testBinarySearchTree = getTestTree(new int[]{0,2,4,6,8,12,14,16,18,20}, 10);
29 |
30 | testBinarySearchTree.insertValue(3);
31 | testBinarySearchTree.recursiveInsert(testBinarySearchTree.getRoot(), 17);
32 |
33 | assertTrue(testBinarySearchTree.searchValue(testBinarySearchTree.getRoot(), 3));
34 | assertTrue(testBinarySearchTree.searchValue(testBinarySearchTree.getRoot(), 17));
35 |
36 | }
37 |
38 | @Test
39 | public void testSearchValue() throws Exception {
40 | BinarySearchTree testBinarySearchTree = getTestTree(new int[]{0,2,4,6,8,12,14,16,18,20}, 10);
41 |
42 | assertTrue(testBinarySearchTree.searchValue(testBinarySearchTree.getRoot(), 4));
43 | assertFalse(testBinarySearchTree.searchValue(testBinarySearchTree.getRoot(), 5));
44 | }
45 |
46 | @Test
47 | public void testPreOrderTraversal() throws Exception {
48 | BinarySearchTree binarySearchTree = getTestTree(new int[]{17, 1, 11, 65, 99, 0, 12, 4, 18, 2, 121}, 50);
49 | ArrayList actualTraversedList = Lists.newArrayList();
50 | ArrayList expectedTraversedList = Lists.newArrayList(50,17,1,0,11,4,2,12,18,65,99,121);
51 |
52 | binarySearchTree.preOrderTreeTraversal(binarySearchTree.getRoot(), actualTraversedList);
53 |
54 | assertThat(actualTraversedList.size(), equalTo(expectedTraversedList.size()));
55 | for(int i = 0; i actualTraversedList = Lists.newArrayList();
64 | ArrayList expectedTraversedList = Lists.newArrayList(0,1,2,4,11,12,17,18,50,65,99,121);
65 |
66 | binarySearchTree.inOrderTreeTraversal(binarySearchTree.getRoot(), actualTraversedList);
67 |
68 | assertThat(actualTraversedList.size(), equalTo(expectedTraversedList.size()));
69 | for(int i = 0; i actualTraversedList = Lists.newArrayList();
78 | ArrayList expectedTraversedList = Lists.newArrayList(0,2,4,12,11,1,18,17,121,99,65,50);
79 |
80 | binarySearchTree.postOrderTreeTraversal(binarySearchTree.getRoot(), actualTraversedList);
81 |
82 | assertThat(actualTraversedList.size(), equalTo(expectedTraversedList.size()));
83 | for(int i = 0; i actualTraversedList = Lists.newArrayList();
92 | ArrayList expectedTraversedList = Lists.newArrayList(50, 17, 65, 1, 18, 99, 0, 11, 121, 4, 12, 2);
93 |
94 | binarySearchTree.breadthFirstTraversal(binarySearchTree.getRoot(), actualTraversedList);
95 |
96 | assertThat(actualTraversedList.size(), equalTo(expectedTraversedList.size()));
97 | for(int i = 0; i testList = Lists.newArrayList(5,2,11,6,4,16,13,1,19,12,3);
16 | bubbleSort.sortList(testList);
17 | ArrayList expectedList = Lists.newArrayList(1, 2, 3, 4, 5, 6, 11, 12, 13, 16, 19);
18 | assertThat(testList.equals(expectedList), equalTo(true));
19 | }
20 | }
21 |
--------------------------------------------------------------------------------