├── .editorconfig ├── .gitignore ├── .gitmodules ├── .jshintignore ├── CONTRIBUTING.md ├── LICENSE ├── Makefile ├── README.md ├── composer.json ├── package.json ├── phpunit.xml ├── 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 ├── closest-sum │ └── problem.txt ├── 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-common-prefix │ └── 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 ├── queen-threatens-king │ ├── queen-threatens-king.png │ └── 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 ├── shop-in-candy-store │ └── Readme.md ├── shortest-fizz-buzz │ └── Readme.md ├── skiing-in-singapore │ └── README.md ├── sorted-array-search │ └── Readme.md ├── spiral │ ├── Readme.md │ ├── input-1.png │ └── input-2.png ├── spreadsheet │ └── README.md ├── stack-machine │ └── Readme.md ├── stack │ └── Readme.md ├── string-format │ └── Readme.md ├── string-permutations │ └── Readme.md ├── string-rotation │ └── Readme.md ├── sum-of-array-plus-one │ └── Readme.md ├── throttle │ └── Readme.md ├── transform-word │ └── Readme.md ├── transitive-dependencies │ └── Readme.md ├── tree-level-order-print │ └── Readme.md ├── word-analytics │ ├── Readme.md │ └── huckleberry-finn.txt └── word-positions │ └── Readme.md ├── solutions ├── c │ ├── bubble-sort.c │ ├── factorial.c │ ├── find-missing-element.c │ ├── multiples-of-3-and-5.c │ ├── selection-sort.c │ ├── sgski.c │ ├── shortest-fizz-buzz.c │ ├── spreadsheet │ │ ├── 3x2.txt │ │ ├── Makefile │ │ ├── README.md │ │ ├── commons.c │ │ ├── commons.h │ │ ├── matrix.c │ │ ├── matrix.h │ │ ├── matrixTest │ │ ├── regex.c │ │ ├── regex.h │ │ ├── spreadsheet │ │ ├── spreadsheet.c │ │ ├── stackcalc.c │ │ ├── stackcalc.h │ │ └── tests │ │ │ ├── Makefile │ │ │ ├── core │ │ │ ├── matrixTest │ │ │ └── matrix_test.c │ ├── stack.c │ └── stack.h ├── coffeescript │ ├── anagram-detection.coffee │ ├── array-pair-sum.coffee │ ├── balanced-brackets.coffee │ ├── bubble-sort.coffee │ ├── byte-format.coffee │ ├── combine-two-strings.coffee │ ├── csv-parsing.coffee │ ├── debounce.coffee │ ├── even-occuring-element.coffee │ ├── factorial.coffee │ ├── fibonacci.coffee │ ├── find-missing-element.coffee │ ├── first-non-repeated-character.coffee │ ├── flatten-array.coffee │ ├── get-elements-by-class-name.coffee │ ├── hotel-room.coffee │ ├── integer-difference.coffee │ ├── integer-length.coffee │ ├── kth-largest-element-in-array.coffee │ ├── largest-continuous-sum.coffee │ ├── largest-palindrome.coffee │ ├── linked-list.coffee │ └── word-positions.coffee ├── cpp │ ├── ShopInCandyStore.hpp │ ├── once.hpp │ ├── queue.hpp │ ├── spiral.hpp │ └── stack.hpp ├── cs │ ├── hotel-room.cs │ ├── longest-words.cs │ ├── shortest-fizz-buzz.cs │ └── sum-of-array-plus-one.cs ├── elixir │ └── factorial.ex ├── go │ ├── longest-words.go │ ├── merge-sort.go │ ├── multiples-of-3-or-5.go │ ├── shortest-fizz-buzz.go │ ├── stack.go │ └── string-permutations.go ├── java │ ├── AnagramDetection.java │ ├── ArrayPairSum.java │ ├── BalancedBrackets.java │ ├── BinarySearchTree.java │ ├── BinarySearchTreeCheck.java │ ├── BubbleSort.java │ ├── CombineTwoStrings.java │ ├── ConvertArray.java │ ├── EvenOccuringElement.java │ ├── Fibonacci.java │ ├── FindingMissingElement.java │ ├── HotelRoom.java │ ├── IntegerDifference.java │ ├── LargestContinuousSum.java │ ├── LargestPalindrome.java │ ├── LongestCommonPrefix.java │ ├── LongestCompoundWord.java │ ├── MedianIntegerStream.java │ ├── NextHighestNumber.java │ ├── RemoveDuplicatesFromString.java │ ├── ReverseWordsInAString.java │ ├── SearchUnknownLengthArray.java │ ├── Spiral.java │ ├── StringRotation.java │ └── TreeLevelOrderPrint.java ├── javascript │ ├── anagram-detection.js │ ├── array-pair-sum.js │ ├── balanced-brackets.js │ ├── binary-search-tree-check.js │ ├── binary-search-tree.js │ ├── bubble-sort.js │ ├── byte-format.js │ ├── combine-two-strings.js │ ├── convert-array.js │ ├── csv-parsing.js │ ├── debounce.js │ ├── even-occuring-element.js │ ├── factorial.js │ ├── fibonnaci.js │ ├── find-missing-element.js │ ├── first-non-repeated-character.js │ ├── flatten-array.js │ ├── flatten-array │ │ ├── copy.js │ │ ├── es5.js │ │ ├── in-place.js │ │ └── recursive.js │ ├── get-elements-by-class-name.js │ ├── hotel-room.js │ ├── insertion-sort.js │ ├── integer-difference.js │ ├── integer-length.js │ ├── kth-largest-element-in-array.js │ ├── largest-continuous-sum.js │ ├── largest-palindrome.js │ ├── linked-list.js │ ├── longest-compound-word.js │ ├── longest-words.js │ ├── matching-nodes.js │ ├── median-integer-stream.js │ ├── merge-sort.js │ ├── missing-number.js │ ├── money-format.js │ ├── multiples-of-3-and-5.js │ ├── next-highest-number.js │ ├── next-palindrome-number.js │ ├── number-format.js │ ├── odd-occuring-element.js │ ├── once.js │ ├── prime-number.js │ ├── queen-threatens-king.js │ ├── queen-threatens-king │ │ ├── queen-threatens-king-solution-diagonal-1.png │ │ ├── queen-threatens-king-solution-diagonal-2.png │ │ ├── queen-threatens-king-solution-horizontal.png │ │ ├── queen-threatens-king-solution-vertical.png │ │ └── readme.md │ ├── queue.js │ ├── quick-sort.js │ ├── remove-duplicates-from-string.js │ ├── reverse-words-in-string.js │ ├── search-unknown-length-array.js │ ├── selection-sort.js │ ├── shortest-fizz-buzz.js │ ├── sorted-array-search.js │ ├── spiral.js │ ├── stack.js │ ├── string-format.js │ ├── string-permutations.js │ ├── string-rotation.js │ ├── sum-of-array-plus-one.js │ ├── throttle.js │ ├── transform-word.js │ ├── tree-level-order-print.js │ ├── word-analytics.js │ └── word-positions.js ├── perl │ ├── anagram-detection.pl │ └── shortest-fizz-buzz.pl ├── php │ ├── factorial.php │ ├── hotel-room.php │ ├── integer-length.php │ ├── reverse-words-in-string.php │ ├── shortest-fizz-buzz.php │ ├── sum-of-array-plus-one.php │ └── transitive-dependencies.php ├── python │ ├── anagram-detection.py │ ├── array-pair-sum.py │ ├── balanced-brackets.py │ ├── bubble-sort.py │ ├── closetsum.py │ ├── convert-array.py │ ├── csv-parsing.py │ ├── even-occuring-element.py │ ├── factorial.py │ ├── fibonacci.py │ ├── find-missing-element.py │ ├── first-non-repeated-character.py │ ├── flatten-array.py │ ├── largest-continuous-sum.py │ ├── multiples-of-3-and-5.py │ ├── once.py │ ├── prime-number.py │ ├── quick-sort.py │ ├── shortest-fizz-buzz.py │ ├── string-permutations.py │ ├── sum-of-array-plus-one.py │ └── tree-level-order-print.py ├── ruby │ ├── anagram_detection.rb │ ├── array_pair_sum.rb │ ├── balanced_brackets.rb │ ├── bubble_sort.rb │ ├── factorial.rb │ ├── fib.rb │ ├── fibonacci.rb │ ├── flatten_array.rb │ ├── int_length.rb │ ├── longest_words.rb │ ├── merge_sort.rb │ ├── multiples_of_three_and_five.rb │ ├── odd_occuring_element.rb │ ├── prime_number.rb │ ├── quick_sort.rb │ ├── remove_duplicates.rb │ ├── reverse_words.rb │ ├── selection_sort.rb │ ├── shortest_fizz_buzz.rb │ ├── sum_of_array_plus_one.rb │ ├── sum_plus_one.rb │ └── word_position.rb └── scala │ ├── AnagramDetection.scala │ ├── Factorial.scala │ └── Fibonacci.scala └── tests ├── cpp ├── common.cpp ├── common.hpp ├── once.cpp ├── queue.cpp ├── spiral.cpp └── stack.cpp ├── javascript ├── anagram-detection.js ├── array-pair-sum.js ├── balanced-brackets.js ├── binary-search-tree-check.js ├── binary-search-tree.js ├── bubble-sort.js ├── byte-format.js ├── combine-two-strings.js ├── convert-array.js ├── factorial.js ├── flatten-array.js ├── insertion-sort.js ├── largest-continuous-sum.js ├── next-highest-number.js ├── prime-number.js ├── queen-threatens-king.js ├── quick-sort.js ├── search-unknown-length-array.js ├── sorted-array-search.js └── word-positions.js └── php ├── IntegerLengthTest.php ├── factorialTest.php ├── reverseWordsInStringTest.php └── sumOfArrayPlusOneTest.php /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | 18 | [Makefile] 19 | indent_style = tab 20 | indent_size = 4 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | node_modules 4 | *.o 5 | vendor 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests/cpp/gtest"] 2 | path = tests/cpp/gtest 3 | url = git@github.com:tclamb/gtest.git 4 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | solutions/javascript/shortest-fizz-buzz.js 2 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Structure 4 | 5 | ### Adding a new problem 6 | 7 | To add a new problem to the repo, no solution needs to be present. Just add a folder to the problems directory with a readme detailing the complete problem and expected result. 8 | 9 | ### Adding a new solution 10 | 11 | To add a new solution to the repo, add the solution to the correct language directory in the solutions directory. Make the solution file name match the problem. If a solution already exists, but your solution is distinctly different to the current solution, place both solutions in a directory named after the problem. The file names should then accurately represent the type of solution used in each solution. If you must use multiple files for your solution, create a solution directory and another directory for your solution assets. 12 | 13 | ## Style Guide 14 | 15 | In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. 16 | 17 | Please lint and test your code with any means available - currently JavaScript has tests and linting via Mocha and JSHint. 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Blake Embrey (hello@blakeembrey.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /bin/sh 2 | TEST_DIR := tests 3 | GTEST_DIR := $(TEST_DIR)/cpp/gtest 4 | 5 | 6 | # C++11 tests 7 | CPP_TESTS := stack-machine.cpp once.cpp stack.cpp queue.cpp spiral.cpp 8 | CPP_SOURCES := $(addprefix $(TEST_DIR)/cpp/, $(CPP_TESTS)) 9 | 10 | 11 | # OS specific settings 12 | UNAME_S = $(shell uname -s) 13 | ifeq ($(UNAME_S),Darwin) 14 | CXXFLAGS += -std=c++11 -stdlib=libc++ 15 | LDFLAGS += -lc++ 16 | CXX := clang++ 17 | else ifeq ($(UNAME_S),Linux) 18 | CXXFLAGS += --std=c++11 -g 19 | CXX := g++ 20 | endif 21 | 22 | 23 | # Languages 24 | default: 25 | @echo "Usage: make " 26 | @echo "Available languages: cpp, js" 27 | 28 | 29 | # Not recommended 30 | .IGNORE: cpp js 31 | all: .IGNORE 32 | 33 | 34 | # C++ build rules 35 | OTHER_SOURCES := $(TEST_DIR)/cpp/common.cpp 36 | FUSED_GTEST_SOURCES := $(GTEST_DIR)/gtest-all.cc $(GTEST_DIR)/gtest_main.cc 37 | CXXFLAGS += -DGTEST_HAS_PTHREAD=0 -DGTEST_HAS_TR1_TUPLE=0 -g 38 | CPP_OBJECTS := $(CPP_SOURCES:.cpp=.o) 39 | FUSED_GTEST_OBJECTS := $(FUSED_GTEST_SOURCES:.cc=.o) 40 | OTHER_OBJECTS := $(OTHER_SOURCES:.cpp=.o) 41 | cpp: hasGTest $(FUSED_GTEST_OBJECTS) $(CPP_OBJECTS) $(OTHER_OBJECTS) 42 | @$(CXX) $(LDFLAGS) \ 43 | $(CPP_OBJECTS) $(FUSED_GTEST_OBJECTS) $(OTHER_OBJECTS)\ 44 | -o cpp 45 | @./cpp 46 | @rm -rf cpp 47 | hasGTest: 48 | @if [ ! -f $(GTEST_DIR)/gtest.h ]; then \ 49 | git submodule update --init -f $(GTEST_DIR); \ 50 | fi 51 | .cpp.o: 52 | @$(CXX) $(CXXFLAGS) -c $< -o $@ 53 | .cc.o: 54 | @$(CXX) $(CXXFLAGS) -c $< -o $@ 55 | 56 | 57 | # JS build rules 58 | js: 59 | @npm install 2> /dev/null 60 | @npm test 61 | 62 | 63 | # Cleanup 64 | clean: 65 | @rm -f $(CPP_OBJECTS) $(FUSED_GTEST_OBJECTS) 66 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "phpunit/phpunit": "5.5.*" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "code-problems", 3 | "private": true, 4 | "description": "Repository full of code problems and common solutions", 5 | "devDependencies": { 6 | "chai": "^3.5.0", 7 | "mocha": "^3.2.0" 8 | }, 9 | "scripts": { 10 | "test": "mocha tests/javascript" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git://github.com/blakeembrey/code-problems.git" 15 | }, 16 | "keywords": [ 17 | "code", 18 | "problems", 19 | "interview", 20 | "programming" 21 | ], 22 | "author": "Blake Embrey", 23 | "license": "MIT", 24 | "readmeFilename": "README.md", 25 | "bugs": { 26 | "url": "https://github.com/blakeembrey/code-problems/issues" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests/php 5 | 6 | -------------------------------------------------------------------------------- /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 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 | -------------------------------------------------------------------------------- /problems/array-pair-sum/Readme.md: -------------------------------------------------------------------------------- 1 | # Array Pair Sum 2 | 3 | Given an integer array, output all distinct 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]) // [ [4, 6], [3, 7] ] 11 | f(8, [3, 4, 5, 4, 4]) // [ [3, 5], [4, 4], [4, 4], [4, 4] ] 12 | f(10, [3, 5, 6, 8]) // [] 13 | ``` 14 | 15 | ## Source 16 | 17 | [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/) 18 | -------------------------------------------------------------------------------- /problems/balanced-brackets/Readme.md: -------------------------------------------------------------------------------- 1 | # Balanced Brackets 2 | 3 | Write a function that accepts a string consisting entirely 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/bubble-sort/Readme.md: -------------------------------------------------------------------------------- 1 | # Bubble Sort 2 | 3 | Implement the [bubble sort algorithm](http://en.wikipedia.org/wiki/Bubble_sort). 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/closest-sum/problem.txt: -------------------------------------------------------------------------------- 1 | Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. 2 | Return the sum of the three integers. 3 | You may assume that each input would have exactly one solution. 4 | 5 | Example: 6 | given array S = {-1 2 1 -4}, 7 | and target = 1. 8 | 9 | The sum that is closest to the target is 2. (-1 + 2 + 1 = 2) -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/insertion-sort/README.md: -------------------------------------------------------------------------------- 1 | # Insertion Sort 2 | 3 | Implement the [insertion sort algorithm](http://en.wikipedia.org/wiki/Insertion_sort). 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/linked-list/Readme.md: -------------------------------------------------------------------------------- 1 | # Linked List 2 | 3 | Write a linked list implementation, better yet - make it doubly linked. 4 | -------------------------------------------------------------------------------- /problems/longest-common-prefix/Readme.md: -------------------------------------------------------------------------------- 1 | # Longest Common Prefix 2 | 3 | Given a String array, find the longest common prefix. 4 | 5 | ## Example 6 | 7 | ``` 8 | f([‘rocket’, ‘rockstar’, ‘rockbottom’, ‘rock’, ‘rollingstone’] // ‘ro’ 9 | f([‘shuffle’, ‘shuttle’, ‘shut’] // ‘shu’ 10 | ``` 11 | 12 | ## Source 13 | Personal Phone Interview 14 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/merge-sort/Readme.md: -------------------------------------------------------------------------------- 1 | # Merge Sort 2 | 3 | Implement the [merge sort algorithm](http://en.wikipedia.org/wiki/Merge_sort). 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/queen-threatens-king/queen-threatens-king.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/problems/queen-threatens-king/queen-threatens-king.png -------------------------------------------------------------------------------- /problems/queen-threatens-king/readme.md: -------------------------------------------------------------------------------- 1 | # The Queen threatens the King 2 | 3 | ![Chessboard](queen-threatens-king.png) 4 | 5 | Given an imaginary chess board of size _m_ x _n_ including only the Queen and the King write a function that takes the positions (coordinates) of both the Queen and the King as parameters and returns a boolean telling whether or not the King is threatened by the Queen: 6 | 7 | ```js 8 | function threatens(qX, qY, kX, kY) { 9 | // ... 10 | } 11 | ``` 12 | 13 | Extra: how would you modify your solution if the rest of the chessmen were included in the board? 14 | 15 | If you are not familiar with the rules of chess, please refer to the [related Wikipedia page](http://en.wikipedia.org/wiki/Queen_(chess)) to learn how the Queen can move around the chess board and threaten other chessmen. 16 | -------------------------------------------------------------------------------- /problems/queue/Readme.md: -------------------------------------------------------------------------------- 1 | # Basic Queue 2 | 3 | Implement a basic queue function with the ability to `add` and `remove` values. 4 | -------------------------------------------------------------------------------- /problems/quick-sort/Readme.md: -------------------------------------------------------------------------------- 1 | # Quick Sort 2 | 3 | Implement the [quick sort algorithm](http://en.wikipedia.org/wiki/Quicksort). 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/selection-sort/readme.md: -------------------------------------------------------------------------------- 1 | # Selection Sort 2 | 3 | Implement the [selection sort algorithm](http://en.wikipedia.org/wiki/Selection_sort). 4 | -------------------------------------------------------------------------------- /problems/shop-in-candy-store/Readme.md: -------------------------------------------------------------------------------- 1 | # Shop in Candy Store 2 | 3 | In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you. 4 | You are now provided with an attractive offer. 5 | You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free. 6 | Now you have to answer two questions. Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies. 7 | In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free. 8 | 9 | 10 | # Input 11 | The first line of the input contains T the number of test cases. Each test case consists of two lines. The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies. 12 | 13 | 14 | # Output 15 | For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent. 16 | Remember to output the answer of each test case in a new line. 17 | 18 | # Constraints 19 | 1 <= T <= 50 20 | 1 <= N <= 1000 21 | 0 <= K <= N-1 22 | 1 <= Ai <= 100 23 | 24 | # Example: 25 | Input 26 | 1 27 | 4 2 28 | 3 2 1 4 29 | 30 | Output 31 | 3 7 32 | 33 | # Explanation 34 | As according to the offer if you but one candy you can take atmost two more for free. 35 | So in the first case you buy the candy which costs 1 and take candies worth 3 and 4 for free, also you buy candy worth 2 as well. 36 | So min cost = 1+2 =3. 37 | In the second case I buy the candy which costs 4 and take candies worth 1 and 2 for free, also I buy candy worth 3 as well. 38 | So max cost = 3+4 =7. 39 | 40 | # Topics : 41 | Greedy || Sorting -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | ![Grid 1](input-1.png) 18 | 19 | ![Grid 2](input-2.png) 20 | -------------------------------------------------------------------------------- /problems/spiral/input-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/problems/spiral/input-1.png -------------------------------------------------------------------------------- /problems/spiral/input-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/problems/spiral/input-2.png -------------------------------------------------------------------------------- /problems/spreadsheet/README.md: -------------------------------------------------------------------------------- 1 | # Programming Challenge 2 | 3 | A spreadsheet consists of a two-dimensional array of cells, labeled A1, A2, etc. Rows are identified using letters, columns by numbers. Each cell contains either an integer (its value) or an expression. Expressions contain integers, cell references, and the operators '+', '-', '\*', '/' with the usual rules of evaluation – note that the input is RPN and should be evaluated in stack order. 4 | 5 | The spreadsheet input is defined as follows: 6 | 7 | 1. Line 1: two integers, defining the width and height of the spreadsheet (n, m) 8 | 9 | 2. n\*m lines each containing an expression which is the value of the corresponding cell (cells enumerated in the order A1, A2, A, B1, ...) 10 | 11 | ## The Input 12 | 3 2 13 | A2 14 | 4 5 * 15 | A1 16 | A1 B2 / 2 + 17 | 3 18 | 39 B1 B2 * / 19 | 20 | ## The Output 21 | 3 2 22 | 20.00000 23 | 20.00000 24 | 20.00000 25 | 8.66667 26 | 3.00000 27 | 1.50000 28 | -------------------------------------------------------------------------------- /problems/stack/Readme.md: -------------------------------------------------------------------------------- 1 | # Basic Stack 2 | 3 | Implement a basic stack function with the ability to `add` and `remove` values. 4 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /problems/transitive-dependencies/Readme.md: -------------------------------------------------------------------------------- 1 | # Transitive Dependencies 2 | 3 | A class that calculates how dependencies propagate between things such as classes in a program. One of the insidious things about dependencies is that they are transitive—if A depends on B and B depends on C, then A also depends on C. So, this code that calculates the full set of dependencies for a group of items. Example class for test: 4 | 5 | ```php 6 | 7 | dependencies = new DependenciesList(); 17 | } 18 | 19 | public function add_direct_test() { 20 | $this->dependencies->add_direct('A', 'B,C'); 21 | $this->dependencies->add_direct('B', 'C,E'); 22 | $this->dependencies->add_direct('C', 'G'); 23 | $this->dependencies->add_direct('D', 'A,F'); 24 | $this->dependencies->add_direct('E', 'F'); 25 | $this->dependencies->add_direct('F', 'H'); 26 | } 27 | 28 | } 29 | 30 | $test = new Dependencies_Test(); 31 | $test->add_direct_test(); 32 | $resultado = $test->dependencies->dependencies_for("A"); 33 | 34 | //print dependencies array in screen 35 | var_dump($resultado); 36 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /solutions/c/bubble-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void bubblesort(int *v){ 4 | 5 | int i,j,aux; 6 | 7 | int size = sizeof(v) / sizeof(v[0]); // Number of indexes in the V array. 8 | 9 | for (i=0; i < size-1; i++) { 10 | for(j=i+1; j < size; j++) { 11 | if(v[i] > v[j]) { 12 | aux=v[i]; 13 | v[i]=v[j]; 14 | v[j]=aux; 15 | } 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /solutions/c/factorial.c: -------------------------------------------------------------------------------- 1 | /* 2 | Observation: 3 | 4 | C Language doesn't have native support to Big Integer numbers, 5 | so on a x86 processor the maximum factorial number that can be reached is 12 6 | and on a x86_64 is 20. 7 | 8 | If you need to use big integer numbers, I recommend you search about the OpenSSL Big Numbers library. 9 | http://www.openssl.org/docs/crypto/bn.html 10 | 11 | To print a `unsigned long int` number use `%lu` on prinf() function. 12 | 13 | */ 14 | 15 | #include "stdio.h" 16 | 17 | // Interative Loop 18 | unsigned long int factorial (unsigned short int number){ 19 | unsigned long int factorial = 1; 20 | unsigned short int i=2; 21 | 22 | do { 23 | factorial *= i++; 24 | } while (i<=number); 25 | return factorial; 26 | } 27 | 28 | // Interative Reverse Loop 29 | unsigned long int factorialReverse (unsigned short int number){ 30 | unsigned long int factorial = 1; 31 | unsigned short int i; 32 | 33 | for (i = number; i > 0; i--){ 34 | factorial *= i; 35 | } 36 | return factorial; 37 | } 38 | 39 | // Recursive 40 | unsigned long int factorialRecursive(unsigned short int number){ 41 | if (number==1) { 42 | return 1; 43 | } else { 44 | return number * factorialRecursive(number-1); 45 | } 46 | } -------------------------------------------------------------------------------- /solutions/c/find-missing-element.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | // The differnce in sum of two arrays will give us the desired value. 4 | // The other approach could be to look up for element each time but that will increase the time complexity of the program. 5 | int find_missing(int orig[],int On,int shuffled[]) 6 | { 7 | int i,sum=0; 8 | for(i=0;i 2 | 3 | int sum_of_multiples(int n) 4 | { int sum=0; 5 | n=n-1; 6 | sum=((n/3)*((n/3)*3 +3))/2 + ((n/5)*((n/5)*5 +5))/2 - ((n/15)*((n/15)*15 + 15))/2; // sum of airthmetic progressions & De-Morgan's Law 7 | return sum; 8 | } 9 | int main() 10 | { 11 | printf("%d\n",sum_of_multiples(1000)); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /solutions/c/selection-sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void selectionsort(int *v) { 4 | 5 | int i, j, min, aux; 6 | 7 | int size = sizeof(v) / sizeof(v[0]); // Number of indexes in the V array. 8 | 9 | for(i = 0; i < n-1; i++){ 10 | min = i; 11 | for (j = i+1; j < n; j++) 12 | if (v[j] < v[min]) 13 | min = j; 14 | aux = v[i]; 15 | v[i] = v[min]; 16 | v[min] = aux; 17 | } 18 | } -------------------------------------------------------------------------------- /solutions/c/shortest-fizz-buzz.c: -------------------------------------------------------------------------------- 1 | main(i){while(printf("%i\r%s%s\n",i++,i%3?"":"Fizz",i%5?"":"Buzz")-9);} 2 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/3x2.txt: -------------------------------------------------------------------------------- 1 | 3 2 2 | A2 3 | 4 5 * 4 | A1 5 | A1 B2 / 2 + 6 | 3 7 | 39 B1 B2 * / 8 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/Makefile: -------------------------------------------------------------------------------- 1 | 2 | all: spreadsheet 3 | 4 | spreadsheet: spreadsheet.c matrix.c matrix.h stackcalc.h stackcalc.c regex.h regex.c commons.h commons.c 5 | clang -ggdb3 -O0 -std=c99 -Wall -Werror -o spreadsheet spreadsheet.c matrix.c regex.c commons.c stackcalc.c -I/usr/local/include -L/usr/local/lib/ -lpcre2-8 6 | 7 | test: matrix_test.c regex.c commons.c regex.h matrix.h stackcalc.h stackcalc.c 8 | clang -o matrixTest -ggdb3 -Wall -Werror -O0 -std=c99 -I/usr/local/include matrix_test.c matrix.c regex.c commons.c stackcalc.c -L/usr/local/lib/ -lpcre2-8 -lcmocka 9 | 10 | clean: 11 | rm -f *.o a.out spreadsheet core 12 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/README.md: -------------------------------------------------------------------------------- 1 | # Dependencies 2 | 3 | This program depends on the following: 4 | 1. pcre2-10.20 - Pattern matching, regular expression 5 | 2. cmocka-1.0.0 - For Unit Tests 6 | 7 | Please refer to their respective documentations on how to install these libraries into your respective system 8 | 9 | # Execution 10 | It is required that the $LD\_LIBRARY\_PATH environment variable be set to the directory where the pcre.so is located if it is located other than /usr/lib 11 | 12 | 13 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/commons.c: -------------------------------------------------------------------------------- 1 | #include "commons.h" 2 | 3 | int getNumberOfDigits(int n) 4 | { 5 | if (n < 0) return -1; 6 | if (n < 10) return 1; 7 | if (n < 100) return 2; 8 | if (n < 1000) return 3; 9 | if (n < 10000) return 4; 10 | if (n < 100000) return 5; 11 | if (n < 1000000) return 6; 12 | 13 | return -1; 14 | } 15 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/commons.h: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Returns the number of digits in the integer n 4 | */ 5 | int getNumberOfDigits(int n); 6 | 7 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/matrix.h: -------------------------------------------------------------------------------- 1 | 2 | typedef struct 3 | { 4 | char ***cells; 5 | int rows; 6 | int cols; 7 | } Worksheet; 8 | 9 | typedef struct 10 | { 11 | int row; 12 | int col; 13 | } MatrixLocation; 14 | 15 | typedef struct 16 | { 17 | char *cellReference; 18 | } CellReference; 19 | 20 | // The maximum content of a cell (4k) 21 | #define CELL_CONTENT_LIMIT 2048 22 | 23 | #define ROW_TO_ASCII_OFFSET 65 24 | 25 | /** 26 | * Initializes the 2d matrix of size rows * columns. 27 | * 28 | * Returns 0 if an error is encountered. 29 | */ 30 | int initWorksheet(Worksheet *worksheet, int rows, int columns); 31 | 32 | /** 33 | * Closes and releases the resources of the worksheet 34 | */ 35 | int closeWorksheet(Worksheet *w); 36 | 37 | /** 38 | * Returns 1 if a cyclic dependency is detected. 0 if otherwise. 39 | */ 40 | int isCyclicRefError(const Worksheet *worksheet, int row, int col); 41 | 42 | /** 43 | * Sets cellContent into the worksheet 44 | */ 45 | int setValue(Worksheet *worksheet, int row, int column, char *cellContent); 46 | 47 | /** 48 | * Returns the value at row, col 49 | */ 50 | char* getValue(const Worksheet *worksheet, int row, int column); 51 | 52 | /** 53 | * Returns the value at row, col and writes it into the buffer. 54 | * 55 | * Usage: 56 | * 57 | * char *buffer; 58 | * getValue2(w, &buffer, row, column); 59 | * 60 | */ 61 | int getValue2(const Worksheet *w, char **buffer, int row, int column); 62 | 63 | /** 64 | * Returns a pointer to a CellReference structure 65 | */ 66 | CellReference *convertToCellReference(const MatrixLocation *matrixLocation); 67 | 68 | /** 69 | * Returns a pointer to a MatrixLocation structure 70 | */ 71 | MatrixLocation *convertToMatrixLocation(const CellReference *cellReference); 72 | 73 | 74 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/matrixTest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/c/spreadsheet/matrixTest -------------------------------------------------------------------------------- /solutions/c/spreadsheet/regex.c: -------------------------------------------------------------------------------- 1 | #include "regex.h" 2 | 3 | pcre2_code * getCellReferencePattern(void) 4 | { 5 | int errorcode = 0; 6 | PCRE2_SIZE erroroffset; 7 | pcre2_code *re = pcre2_compile((PCRE2_SPTR8) CELL_REFERENCE_PATTERN, -1, 0, 8 | &errorcode, &erroroffset, NULL); 9 | if (re == NULL) 10 | { 11 | return 0; 12 | } 13 | 14 | return re; 15 | } 16 | 17 | pcre2_code *compilePattern(char *pattern) 18 | { 19 | int errorcode = 0; 20 | PCRE2_SIZE erroroffset; 21 | pcre2_code *re = pcre2_compile((PCRE2_SPTR8) pattern, -1, 0, 22 | &errorcode, &erroroffset, NULL); 23 | if (re == NULL) 24 | { 25 | return 0; 26 | } 27 | 28 | return re; 29 | } 30 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/regex.h: -------------------------------------------------------------------------------- 1 | #define PCRE2_CODE_UNIT_WIDTH 8 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | static const char *CELL_REFERENCE_PATTERN = "[A-Za-z]\\d+"; 9 | 10 | pcre2_code *getCellReferencePattern(void); 11 | 12 | /** 13 | * Creates a regular expression pattern based on {pattern} 14 | */ 15 | pcre2_code *compilePattern(char *pattern); 16 | 17 | 18 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/spreadsheet: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/c/spreadsheet/spreadsheet -------------------------------------------------------------------------------- /solutions/c/spreadsheet/stackcalc.c: -------------------------------------------------------------------------------- 1 | #define PCRE2_CODE_UNIT_WIDTH 8 2 | 3 | #include 4 | #include 5 | 6 | #include "matrix.h" 7 | #include "stackcalc.h" 8 | 9 | void push(Stack *stack, Node *node) 10 | { 11 | // create new nodelist head 12 | node->next = stack->nodeList; 13 | stack->nodeList = node; 14 | stack->len++; 15 | } 16 | 17 | Node *pop(Stack *stack) 18 | { 19 | Node *nodeList = stack->nodeList; 20 | Node *newHead = nodeList->next; 21 | 22 | // remove reference to the next 23 | nodeList->next = NULL; 24 | stack->nodeList = newHead; 25 | 26 | return nodeList; 27 | } 28 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/stackcalc.h: -------------------------------------------------------------------------------- 1 | 2 | typedef struct Node 3 | { 4 | double value; 5 | struct Node *next; 6 | } Node; 7 | 8 | typedef struct 9 | { 10 | Node *nodeList; 11 | int len; 12 | } Stack; 13 | 14 | /** 15 | * Pushes node into the stack 16 | */ 17 | void push(Stack *stack, Node *node); 18 | 19 | /** 20 | * Pops a node from the stack. Returns NULL if stack is empty 21 | */ 22 | Node *pop(Stack *stack); 23 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/tests/Makefile: -------------------------------------------------------------------------------- 1 | all: tests 2 | 3 | tests: matrix_test.c ../regex.c ../commons.c ../regex.h ../matrix.h ../stackcalc.h 4 | clang -o matrixTest -ggdb3 -Wall -Werror -O0 -std=c99 -I/usr/local/include matrix_test.c ../matrix.c ../regex.c ../commons.c ../stackcalc.c -L/usr/local/lib/ -lpcre2-8 -lcmocka 5 | 6 | 7 | -------------------------------------------------------------------------------- /solutions/c/spreadsheet/tests/core: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/c/spreadsheet/tests/core -------------------------------------------------------------------------------- /solutions/c/spreadsheet/tests/matrixTest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/c/spreadsheet/tests/matrixTest -------------------------------------------------------------------------------- /solutions/c/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "stack.h" 3 | 4 | 5 | void _add(Stack *head,int value){ 6 | Stack node = (Stack)malloc(sizeof(Stack)); 7 | if(node !=NULL){ 8 | node->value = value; 9 | node->next = *head; 10 | *head = node; 11 | } 12 | } 13 | 14 | int _remove(Stack *head){ 15 | int value = -1; 16 | if(head!=NULL){ 17 | Stack top = *head; 18 | value = (*head)->value; 19 | *head = (*head)->next; 20 | free(top); 21 | 22 | } 23 | return value; 24 | } 25 | -------------------------------------------------------------------------------- /solutions/c/stack.h: -------------------------------------------------------------------------------- 1 | #ifndef STACK_H_INCLUDED 2 | #define STACK_H_INCLUDED 3 | 4 | typedef struct Nodestack { 5 | int value; 6 | struct Nodestack *next; 7 | }NodeStack; 8 | 9 | typedef NodeStack *Stack; 10 | 11 | // add the element onto the stack 12 | void _add(Stack*,int); 13 | 14 | //returns the element on top of the stack 15 | int _remove(Stack*); 16 | 17 | 18 | #endif // STACK_H_INCLUDED 19 | -------------------------------------------------------------------------------- /solutions/coffeescript/anagram-detection.coffee: -------------------------------------------------------------------------------- 1 | # Simple function that will take a string of latin characters and return a unique hash 2 | hashString = (str) -> 3 | # Map characters to prime numbers to multiply 4 | charMap = 5 | a: 2 6 | b: 3 7 | c: 5 8 | d: 7 9 | e: 11 10 | f: 13 11 | g: 17 12 | h: 19 13 | i: 23 14 | j: 29 15 | k: 31 16 | l: 37 17 | m: 41 18 | n: 43 19 | o: 47 20 | p: 53 21 | q: 59 22 | r: 61 23 | s: 67 24 | t: 71 25 | u: 73 26 | v: 79 27 | w: 83 28 | x: 89 29 | y: 97 30 | z: 101 31 | A: 103 32 | B: 107 33 | C: 109 34 | D: 113 35 | E: 127 36 | F: 131 37 | G: 137 38 | H: 139 39 | I: 149 40 | J: 151 41 | K: 163 42 | L: 167 43 | M: 173 44 | N: 179 45 | O: 181 46 | P: 191 47 | Q: 193 48 | R: 197 49 | S: 199 50 | T: 211 51 | U: 223 52 | V: 227 53 | W: 229 54 | X: 233 55 | Y: 239 56 | Z: 241 57 | 58 | str.split("").reduce ((memo, char) -> memo * charMap[char]), 1 59 | 60 | anagramDetection = (parent, child) -> 61 | length = child.length 62 | anagram = hashString(child) 63 | total = 0 64 | i = 0 65 | 66 | while i < (parent.length - length) 67 | total += 1 if hashString(parent.substr(i, length)) is anagram 68 | i++ 69 | total 70 | -------------------------------------------------------------------------------- /solutions/coffeescript/array-pair-sum.coffee: -------------------------------------------------------------------------------- 1 | # This is just a modification of the integer difference problem presented elsewhere 2 | arrayPairSum = (k, array) -> 3 | hash = {} 4 | pairs = [] 5 | 6 | # Loop through the array once, storing the results in an object for a 7 | # time complexity of O(n) - the naive solution consists of two for loops 8 | # which results in a complexity of O(n^2) 9 | array.forEach (number) -> 10 | # Make sure the value in unused and it's a unique pair 11 | if hash[k - number] is false and k - number isnt number 12 | pairs.push [number, k - number] 13 | hash[k - number] = true # Set it to "used" 14 | # If the hash value is not true, set the hash to "unused" 15 | not hash[k - number] and (hash[number] = false) 16 | pairs 17 | -------------------------------------------------------------------------------- /solutions/coffeescript/balanced-brackets.coffee: -------------------------------------------------------------------------------- 1 | # Use an object to map sets of brackets to their opposites 2 | brackets = 3 | "(": ")" 4 | "{": "}" 5 | "[": "]" 6 | 7 | # On each input string, process it using the balance checker 8 | balancedBrackets = (string) -> 9 | stack = [] 10 | 11 | # Process every character on input 12 | i = 0 13 | 14 | while i < string.length 15 | if brackets[stack[stack.length - 1]] is string[i] 16 | stack.pop() 17 | else 18 | stack.push string[i] 19 | i++ 20 | not stack.length 21 | -------------------------------------------------------------------------------- /solutions/coffeescript/bubble-sort.coffee: -------------------------------------------------------------------------------- 1 | bubbleSort = (array, compare) -> 2 | # Not an array, empty or array of 1 is already sorted 3 | return array if not Array.isArray(array) or array.length < 2 4 | swap = (array, first, second) -> 5 | temp = array[first] 6 | array[first] = array[second] 7 | array[second] = temp 8 | array 9 | 10 | # Create a compare func if not passed in 11 | if typeof compare isnt "function" 12 | compare = (a, b) -> 13 | (if a > b then 1 else -1) 14 | i = undefined 15 | l = undefined 16 | i = 0 17 | while i < array.length 18 | l = i 19 | swap array, l, l + 1 while l-- and compare(array[l], array[l + 1]) > 0 20 | i++ 21 | array 22 | -------------------------------------------------------------------------------- /solutions/coffeescript/byte-format.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (bytes, precision) -> 2 | suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] 3 | factor = Math.pow(10, (if precision > 0 then precision else 2)) 4 | 5 | # Using a for loop since it's perfect for this kind of problem 6 | i = bytes 7 | k = 0 8 | 9 | while i >= 1024 and k < suffixes.length 10 | i /= 1024 11 | k++ 12 | 13 | # Return the number rounded to precision 14 | (Math.round(i * factor) / factor) + " " + suffixes[k] 15 | -------------------------------------------------------------------------------- /solutions/coffeescript/combine-two-strings.coffee: -------------------------------------------------------------------------------- 1 | combineTwoStrings = (str1, str2, combined) -> 2 | # Generate all the posible paths between `str1` and `str2` 3 | paths = {} 4 | 5 | # Check the string lengths are the same to begin 6 | return false if (str1 + str2).length isnt combined.length 7 | 8 | # Finding paths is essentially the anagrams solution 9 | (findPath = (str1, str2, path) -> 10 | return paths[path] = true if path.length is combined.length 11 | 12 | # Find the next path from the first character of either strings 13 | str1 and findPath(str1.substr(1), str2, path + str1.substr(0, 1)) 14 | str2 and findPath(str1, str2.substr(1), path + str2.substr(0, 1)) 15 | ) str1, str2, "" 16 | combined of paths 17 | -------------------------------------------------------------------------------- /solutions/coffeescript/csv-parsing.coffee: -------------------------------------------------------------------------------- 1 | # Elegant solution using built-in JavaScript functions 2 | parseCSV = (csv) -> 3 | JSON.parse "[" + csv + "]" 4 | 5 | # Crazy parser which was the original solution 6 | parseCSV = (csv) -> 7 | isNumber = false 8 | isInput = false 9 | curr = "" 10 | stack = [] 11 | i = 0 12 | char = undefined 13 | pushStack = undefined 14 | csv = csv.trim() 15 | pushStack = (input) -> 16 | isNumber and (input = +input) 17 | 18 | # Resets 19 | curr = "" 20 | isInput = false 21 | isNumber = false 22 | stack.push input 23 | 24 | while char = csv.charAt(i++) 25 | if char is "\"" 26 | isInput = not curr 27 | else if char is "," 28 | if isInput and not isNumber 29 | curr += char 30 | else 31 | pushStack curr 32 | else if (isNumber or not curr) and not Number.isNaN(+char) 33 | curr += char 34 | isInput = true 35 | isNumber = true 36 | else 37 | throw new Error("Unexpected character") if isNumber or not isInput 38 | curr += char 39 | 40 | # Push the trailing entry 41 | pushStack curr 42 | stack 43 | -------------------------------------------------------------------------------- /solutions/coffeescript/debounce.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (fn, delay, execAsap) -> 2 | timeout = undefined # Keep a reference to the timeout outside the function 3 | -> 4 | 5 | # Keep the functions execution context and arguments in tact 6 | that = this 7 | args = arguments_ 8 | 9 | # If we already have a function ready to execute, clear it 10 | # Else if we are allowed to execute immediately, call the function 11 | if timeout 12 | clearTimeout timeout 13 | else fn.apply that, args if execAsap 14 | timeout = setTimeout(-> 15 | execAsap or fn.apply(that, args) 16 | timeout = null 17 | , delay or 100) 18 | -------------------------------------------------------------------------------- /solutions/coffeescript/even-occuring-element.coffee: -------------------------------------------------------------------------------- 1 | evenOccuringElement = (array) -> 2 | hash = {} 3 | 4 | # Loop though the array adding all the elements together in a hash 5 | array.forEach (num) -> 6 | hash[num] = hash[num] + 1 or 1 7 | 8 | # Loop through all the keys in the hash, returning the number if we have an 9 | # even number of occurances 10 | for i of hash 11 | return Number(i) unless hash[i] & 1 12 | false 13 | -------------------------------------------------------------------------------- /solutions/coffeescript/factorial.coffee: -------------------------------------------------------------------------------- 1 | # Recursive function example 2 | exports.recursive = factorial = (number) -> 3 | return 1 if number < 2 4 | number * factorial(number - 1) 5 | 6 | # Iterative solution 7 | exports.iterative = (number) -> 8 | result = 1 9 | i = 1 10 | 11 | while i <= number 12 | result *= i 13 | i++ 14 | result 15 | 16 | # Iterative using a reverse loop 17 | exports.iterativeReverse = (number) -> 18 | result = 1 19 | while number 20 | result *= number 21 | number -= 1 22 | result 23 | -------------------------------------------------------------------------------- /solutions/coffeescript/fibonacci.coffee: -------------------------------------------------------------------------------- 1 | # Implementing recursive solution 2 | fibonacci = (n) -> 3 | return fibonacci[n] if n of fibonacci 4 | 5 | # Store the fibonacci values on the function itself 6 | fibonacci[n] = (if (n < 2) then n else fibonacci(n - 1) + fibonacci(n - 2)) 7 | 8 | # Implementing iterative solution 9 | fibonacci = (n) -> 10 | results = [0, 1] 11 | if n > 2 12 | i = 2 13 | 14 | while i < n 15 | results[i] = results[i - 2] + results[i - 1] 16 | i++ 17 | results[n - 1] 18 | 19 | # Implementing O(logn) matrix solution 20 | fibonacci = (n) -> 21 | memo = [0, [[0, 1], [1, 1]]] 22 | matrixMultiply = (A, B) -> 23 | C = undefined 24 | if Array.isArray(B[0]) 25 | C = [[], []] 26 | C[0][0] = A[0][0] * B[0][0] + A[1][0] * B[0][1] 27 | C[0][1] = A[0][1] * B[0][0] + A[1][1] * B[0][1] 28 | C[1][0] = A[0][0] * B[1][0] + A[1][0] * B[1][1] 29 | C[1][1] = A[0][1] * B[1][0] + A[1][1] * B[1][1] 30 | else 31 | C = [] 32 | C[0] = A[0][0] * B[0] + A[1][0] * B[1] 33 | C[1] = A[0][1] * B[0] + A[1][1] * B[1] 34 | C 35 | 36 | # Calculates fibonacci spiral transformation matrix 37 | calcFibSpiral = (n) -> 38 | count = 1 39 | T = undefined 40 | if n & 1 41 | T = [[0, 1], [1, 1]] 42 | n -= 1 43 | else 44 | T = [[1, 0], [0, 1]] 45 | while n > 0 46 | count++ 47 | memo[count] = matrixMultiply(memo[count - 1], memo[count - 1]) unless memo[count] 48 | T = matrixMultiply(T, memo[count]) if (n >>= 1) & 1 49 | T 50 | 51 | matrixMultiply(calcFibSpiral(n - 2), [1, 1])[1] 52 | -------------------------------------------------------------------------------- /solutions/coffeescript/find-missing-element.coffee: -------------------------------------------------------------------------------- 1 | # Simple solution using a hash to look up numbers from the second array in the 2 | # first array. When the number doesn't exist in the hash - you know we have 3 | # found the missing number 4 | findMissingElement = (a, b) -> 5 | hash = {} 6 | i = undefined 7 | i = 0 8 | while i < b.length 9 | hash[b[i]] = hash[b[i]] + 1 or 1 10 | i++ 11 | i = 0 12 | while i < a.length 13 | return a[i] unless hash[a[i]] 14 | hash[a[i]] -= 1 15 | i++ 16 | 17 | # Bitwise solution using XOR to cancel each of the corresponding numbers out 18 | # with eachother until we end up with a number that isn't cancelled out 19 | findMissingElement = (a, b) -> 20 | result = 0 21 | a.concat(b).forEach (num) -> 22 | result ^= num 23 | result 24 | 25 | # Maybe the simplest solution, but you can very easily add the two arrays and 26 | # take the result of `b` away from `a` to get the missing number 27 | findMissingElement = (a, b) -> 28 | add = (a, b) -> 29 | a + b 30 | a.reduce(add) - b.reduce(add) 31 | -------------------------------------------------------------------------------- /solutions/coffeescript/first-non-repeated-character.coffee: -------------------------------------------------------------------------------- 1 | firstNonRepeatedCharacter = (string) -> 2 | checkChar = undefined 3 | prevCharacter = undefined 4 | checkChar = (-> 5 | repeated = false 6 | (char) -> 7 | repeated = true if prevCharacter and char is prevCharacter 8 | if prevCharacter and char isnt prevCharacter 9 | return true unless repeated 10 | repeated = false 11 | prevCharacter = char 12 | 13 | # Return false to say it's not been repeated 14 | false 15 | )() 16 | 17 | # Interate one extra time past the last character 18 | i = 0 19 | 20 | while i <= string.length 21 | return prevCharacter if checkChar(string[i]) 22 | i++ 23 | -------------------------------------------------------------------------------- /solutions/coffeescript/flatten-array.coffee: -------------------------------------------------------------------------------- 1 | # Create a new flattened array 2 | flatten = (input) -> 3 | output = [] 4 | i = 0 5 | 6 | while i < input.length 7 | # Using Array.isArray for new browsers, in older browsers this can be 8 | # polyfilled using `Object.prototype.toString.call(input[i]) === '[object Array]'` 9 | if Array.isArray(input[i]) 10 | output.push.apply output, flatten(input[i]) 11 | else 12 | output.push input[i] 13 | i++ 14 | output 15 | 16 | # In place array flatten 17 | flatten = (array) -> 18 | i = 0 19 | while i < array.length 20 | if Array.isArray(array[i]) 21 | array.splice.apply array, [i, 1].concat(array[i]) 22 | else 23 | i += 1 24 | array 25 | 26 | # Flatten array using ES5 reduce method 27 | flatten = (array) -> array.reduce ((arr, val) -> arr.concat (if Array.isArray(val) then flatten(val) else val)), [] 28 | -------------------------------------------------------------------------------- /solutions/coffeescript/get-elements-by-class-name.coffee: -------------------------------------------------------------------------------- 1 | getElementsByClassName = (element, className) -> 2 | found = [] 3 | (traverseDom = (node) -> 4 | # Store the current node and current nodes class in a temporary variable 5 | currentNode = undefined 6 | currentClass = undefined 7 | 8 | # Loop through all the child nodes 9 | i = 0 10 | 11 | while i < node.childNodes.length 12 | currentNode = node.childNodes[i] 13 | currentClass = currentNode.className 14 | 15 | # Check if the class name exists within the current nodes class 16 | # I believe I learnt of this technique from jQuery source code 17 | found.push currentNode if currentClass and ~(" " + currentClass + " ").indexOf(" " + className + " ") 18 | 19 | # If the current node have more child nodes, continue traversing 20 | currentNode.childNodes and traverseDom(currentNode) 21 | i++ 22 | ) element 23 | found 24 | 25 | getElementsByClassName = (element, className) -> 26 | # This function takes an easier approach, using a regular expression and 27 | # getting all elements straight up using the asterisk selector 28 | found = [] 29 | regex = new RegExp("(^| )" + className + "($| )") 30 | elements = element.getElementsByTagName("*") 31 | 32 | # Loop through all the elements checking the class names against the 33 | # regular expression - when it suceeds just push it into the output array 34 | i = 0 35 | 36 | while i < elements.length 37 | found.push elements[i] if regex.test(elements[i].className) 38 | i++ 39 | found 40 | -------------------------------------------------------------------------------- /solutions/coffeescript/hotel-room.coffee: -------------------------------------------------------------------------------- 1 | findRoom = (totalRooms) -> 2 | findDivisors = (number) -> 3 | divisors = [] 4 | iterator = number 5 | divisors.push iterator if number % iterator is 0 while iterator-- 6 | divisors 7 | 8 | # Returns true or false based on whether the number is found in the sum of array subsets 9 | isSubsetSum = (number, array) -> 10 | hasSubset = false 11 | (findSubset = (total, numbers) -> 12 | not hasSubset and (hasSubset = total is number) 13 | return if hasSubset or total > number 14 | numbers.forEach (num, index) -> 15 | findSubset total + num, numbers.slice(0, index).concat(numbers.slice(index + 1)) 16 | ) 0, array 17 | hasSubset 18 | 19 | # Need a simple helper method that returns the sum of an array 20 | sumArray = (array) -> 21 | array.reduce ((memo, num) -> 22 | memo + num 23 | ), 0 24 | 25 | # Find the room using the provided functions 26 | divisors = undefined 27 | room = 0 28 | 29 | while room <= totalRooms 30 | divisors = findDivisors(room) 31 | 32 | # The sum of all the divisors must be greater than the number 33 | return room if sumArray(divisors) > room and not isSubsetSum(room, divisors) 34 | room++ 35 | 0 # No room number found 36 | -------------------------------------------------------------------------------- /solutions/coffeescript/integer-difference.coffee: -------------------------------------------------------------------------------- 1 | integerDifference = (n, array) -> 2 | hash = {} 3 | total = 0 4 | 5 | # Loop through the array once, storing the results in an object for a 6 | # time complexity of O(n) - the naive solution consists of two for loops 7 | # which results in a complexity of O(n^2) 8 | array.forEach (number) -> 9 | hash[number] = (hash[number] or 0) + 1 10 | total += (hash[number - n] or 0) + (hash[number + n] or 0) 11 | total 12 | -------------------------------------------------------------------------------- /solutions/coffeescript/integer-length.coffee: -------------------------------------------------------------------------------- 1 | integerLength = (num) -> 2 | # There is an extra check here to ensure the number is an integer 3 | ("" + (num | 0)).length 4 | -------------------------------------------------------------------------------- /solutions/coffeescript/kth-largest-element-in-array.coffee: -------------------------------------------------------------------------------- 1 | kthLargestElementInArray = (k, array) -> 2 | # I believe we can store it in a hash to achieve an O(n) complexity 3 | hash = {} 4 | count = 0 5 | 6 | # Loop through each of the array items putting the values as keys in the hash 7 | array.forEach (num) -> 8 | hash[num] = hash[num] + 1 or 1 9 | 10 | # Loop through each of the keys in the hash and keep track of the total count 11 | for i of hash 12 | # Check if `k` is smaller or equal to the current count plus the current 13 | # hash index, but also greater than the previous count (this will mean it 14 | # is stored in this integer key) 15 | 16 | # Coerce the output back to a number, since that is expected 17 | return +i if k <= count + hash[i] and k > count 18 | 19 | # Increment the total count 20 | count += hash[i] 21 | -1 22 | -------------------------------------------------------------------------------- /solutions/coffeescript/largest-continuous-sum.coffee: -------------------------------------------------------------------------------- 1 | largestContinuousSum = (array) -> 2 | return if !array or !array.length 3 | 4 | currentSum = maximumSum = array.shift() 5 | 6 | # Checks the array of sums and compares them 7 | array.forEach (num) -> 8 | currentSum = Math.max(currentSum + num, num) 9 | maximumSum = Math.max(currentSum, maximumSum) 10 | maximumSum 11 | -------------------------------------------------------------------------------- /solutions/coffeescript/largest-palindrome.coffee: -------------------------------------------------------------------------------- 1 | largestPalindrome = (str) -> 2 | palindromes = [] 3 | walkPalindrome = (result, str, leftIndex, rightIndex) -> 4 | result = str[leftIndex -= 1] + result + str[rightIndex += 1] while str[leftIndex - 1] is str[rightIndex + 1] 5 | palindromes.push result 6 | i = 0 7 | 8 | while i < str.length 9 | if str[i] is str[i - 1] 10 | walkPalindrome str[i] + str[i - 1], str, i - 1, i 11 | else if str[i] is str[i + 1] 12 | walkPalindrome str[i] + str[i + 1], str, i, i + 1 13 | else walkPalindrome str[i - 1] + str[i] + str[i + 1], str, i - 1, i + 1 if str[i - 1] is str[i + 1] 14 | i++ 15 | palindromes.reduce ((memo, str) -> (if str.length > memo.length then str else memo)), "" 16 | -------------------------------------------------------------------------------- /solutions/coffeescript/linked-list.coffee: -------------------------------------------------------------------------------- 1 | LinkedList = (value) -> 2 | @value = value 3 | @prev = this 4 | @next = this 5 | 6 | LinkedList::appendNode = (value) -> 7 | node = new LinkedList(value) 8 | node.prev = this 9 | node.next = @next 10 | 11 | # Fix the linked list references 12 | @next = @next.prev = node 13 | node 14 | 15 | LinkedList::prependNode = (value) -> 16 | node = new LinkedList(value) 17 | node.prev = @prev 18 | node.next = this 19 | 20 | # Fix the linked list references 21 | @prev = @prev.next = node 22 | node 23 | 24 | LinkedList::removeNode = -> 25 | # Create a reference around the node to be removed 26 | @prev.next = @next 27 | @next.prev = @prev 28 | 29 | # Remove existing references to the current list 30 | @next = @prev = this 31 | this 32 | 33 | LinkedList::containsNode = (value) -> 34 | return true if @value is value 35 | node = @next 36 | 37 | # Loop through the connections until we hit ourselves again 38 | while node isnt this 39 | return true if node.value is value 40 | node = node.next 41 | false 42 | -------------------------------------------------------------------------------- /solutions/coffeescript/word-positions.coffee: -------------------------------------------------------------------------------- 1 | module.exports = (text) -> 2 | trie = {} 3 | pos = 0 4 | active = trie # Start the active structure as the root trie structure 5 | 6 | # Suffix a space after the text to make life easier 7 | text += " " 8 | 9 | # Loop through the input text adding it to the trie structure 10 | i = 0 11 | 12 | while i < text.length 13 | 14 | # When the character is a space, restart 15 | if text[i] is " " 16 | 17 | # If the current active doesn't equal the root, set the position 18 | (active.positions = active.positions or []).push pos if active isnt trie 19 | 20 | # Reset the positions and the active part of the data structure 21 | pos = i 22 | active = trie 23 | continue 24 | 25 | # Set the next character in the structure up 26 | active[text[i]] = (active[text[i]] or {}) 27 | active = active[text[i]] 28 | i++ 29 | 30 | # Return a function that accepts a word and looks it up in the trie structure 31 | (word) -> 32 | i = -1 33 | active = trie 34 | while word[++i] 35 | return [] unless active[word[i]] 36 | active = active[word[i]] 37 | active.positions 38 | -------------------------------------------------------------------------------- /solutions/cpp/ShopInCandyStore.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main() 4 | { 5 | int t; cin>>t; 6 | while(t--) 7 | { 8 | int n,k; cin>>n>>k; long int a[n],min=0,max=0,c=0; 9 | for(int i=0;i>a[i]; 10 | sort(a,a+n); 11 | int x=(n/(k+1)); if((n%(k+1))>0) {x++;} 12 | 13 | for(int i=0;i // std::vector 5 | #include // std::move 6 | 7 | std::vector spiral(unsigned int height, unsigned int width, int row, int column) { 8 | enum { 9 | Up, Left, Down, Right 10 | } currentDirection = Up; 11 | unsigned int maxLength = 1, 12 | length = 0; 13 | std::vector visited; 14 | 15 | while(visited.size() != height*width) { 16 | if(width >= column && column > 0) 17 | if(height >= row && row > 0) 18 | visited.push_back(column + (row - 1)*width); 19 | 20 | switch(currentDirection) { 21 | case Up: 22 | --row; 23 | break; 24 | case Left: 25 | --column; 26 | break; 27 | case Down: 28 | ++row; 29 | break; 30 | case Right: 31 | ++column; 32 | break; 33 | } 34 | 35 | ++length; 36 | 37 | if(length == maxLength) { 38 | length = 0; 39 | 40 | switch(currentDirection) { 41 | case Up: 42 | currentDirection = Left; 43 | break; 44 | case Left: 45 | currentDirection = Down; 46 | ++maxLength; 47 | break; 48 | case Down: 49 | currentDirection = Right; 50 | break; 51 | case Right: 52 | currentDirection = Up; 53 | ++maxLength; 54 | break; 55 | } 56 | } 57 | } 58 | 59 | return std::move(visited); 60 | } 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /solutions/cpp/stack.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STACK_HPP_INCLUDED 2 | #define STACK_HPP_INCLUDED 3 | 4 | #include // std::forward 5 | #include // std::move 6 | #include // std::runtime_error 7 | 8 | class empty_stack : public std::runtime_error { 9 | public: 10 | empty_stack(std::string const& what_arg) : std::runtime_error{what_arg} {}; 11 | }; 12 | 13 | template 14 | class Stack { 15 | public: 16 | Stack() : head{nullptr} {}; 17 | ~Stack(); 18 | 19 | template 20 | void add(U&&); 21 | T remove(); 22 | 23 | inline bool empty() const { return head == nullptr; }; 24 | 25 | private: 26 | struct Node { 27 | T data; 28 | Node* next; 29 | 30 | template 31 | Node(U&& d, Node* n) : data{std::forward(d)}, next{n} {}; 32 | }; 33 | 34 | Node* head; 35 | }; 36 | 37 | template 38 | Stack::~Stack() { 39 | // Rewriting functionality of remove here avoids a call to the move 40 | // constructor for each element in the stack. For code golf, use: 41 | // while(!empty()) remove(); 42 | while(!empty()) { 43 | auto old_head = head; 44 | head = head->next; 45 | delete old_head; 46 | } 47 | } 48 | 49 | template 50 | template 51 | void Stack::add(U&& t) { 52 | head = new Node{std::forward(t), head}; 53 | } 54 | 55 | template 56 | T Stack::remove() { 57 | if(empty()) 58 | throw empty_stack{"Remove called on empty stack."}; 59 | 60 | auto old_head = head; 61 | auto old_data = std::move(head->data); 62 | head = head->next; 63 | 64 | delete old_head; 65 | return old_data; 66 | } 67 | 68 | #endif // STACK_HPP_INCLUDED 69 | -------------------------------------------------------------------------------- /solutions/cs/longest-words.cs: -------------------------------------------------------------------------------- 1 | char[] delimiter = new char[] { ' ' }; 2 | public List FindLongestWords(string sentence) 3 | { 4 | List longestWords = new List(); 5 | int currentLongestLength = 0; 6 | string[] words = sentence.Split(delimiter, StringSplitOptions.RemoveEmptyEntries); 7 | if (words != null && words.Length > 0) 8 | { 9 | foreach (string word in words) 10 | { 11 | // Duplicate check. 12 | if (!longestWords.Contains(word.ToLower())) 13 | { 14 | // If word is longer than the current longest. We clear our word list and add only this one. 15 | if (word.Length > currentLongestLength) 16 | { 17 | longestWords.Clear(); 18 | longestWords.Add(word.ToLower()); 19 | currentLongestLength = word.Length; 20 | } 21 | // If word's length equals currentLongest, we just add it to the list. 22 | else if (word.Length == currentLongestLength) 23 | { 24 | longestWords.Add(word); 25 | } 26 | } 27 | } 28 | } 29 | return longestWords; 30 | } 31 | -------------------------------------------------------------------------------- /solutions/cs/shortest-fizz-buzz.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace fizz_buzz 3 | { 4 | class Program 5 | { 6 | static void Main(string[] args) 7 | { 8 | for (int i = 1; i < 101; i++) 9 | { 10 | if (i % 3 < 1) 11 | Console.Write("fizz"); 12 | if (i % 5 < 1) 13 | Console.Write("buzz"); 14 | if (i % 3 > 0 && i % 5 > 0) 15 | Console.Write("{0}", i); 16 | Console.WriteLine(""); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /solutions/cs/sum-of-array-plus-one.cs: -------------------------------------------------------------------------------- 1 | public int SumOfIntegers(int[] integerArray) 2 | { 3 | int sum = 0; 4 | foreach (int item in integerArray) 5 | { 6 | sum += item; 7 | } 8 | return sum + integerArray.Length; 9 | } 10 | -------------------------------------------------------------------------------- /solutions/elixir/factorial.ex: -------------------------------------------------------------------------------- 1 | defmodule Factorial do 2 | def factorial(n), do: Enum.reduce(1..n, &(&1 * &2)) 3 | end 4 | -------------------------------------------------------------------------------- /solutions/go/longest-words.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | func longest(sentence string) string { 9 | longest := "" 10 | words := make([]string, 0) 11 | words = strings.Split(sentence, " ") 12 | 13 | for _, word := range words { 14 | if len(longest) < len(word) { 15 | longest = word 16 | } 17 | } 18 | 19 | return longest; 20 | } 21 | 22 | func main() { 23 | fmt.Println(longest("You are just an old antidisestablishmentarian")) 24 | } -------------------------------------------------------------------------------- /solutions/go/merge-sort.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "math" 6 | ) 7 | 8 | func mergeSort(ary []int) []int { 9 | if len(ary) <= 1 { 10 | return ary 11 | } 12 | left := make([]int, 0) 13 | right := make([]int, 0) 14 | middle := math.Floor(float64(len(ary) / 2)) 15 | 16 | left = ary[0:int(middle)] 17 | right = ary[int(middle):] 18 | 19 | return merge(mergeSort(left), mergeSort(right)) 20 | } 21 | 22 | func merge(left, right []int) []int { 23 | 24 | result := make([]int, 0) 25 | 26 | for len(left) > 0 || len(right) > 0 { 27 | if len(left) > 0 && len(right) > 0 { 28 | if left[0] <= right[0] { 29 | result = append(result, left[0]) 30 | left = left[1:len(left)] 31 | } else { 32 | result = append(result, right[0]) 33 | right = right[1:len(right)] 34 | } 35 | } else if len(left) > 0 { 36 | result = append(result, left[0]) 37 | left = left[1:len(left)] 38 | } else { 39 | result = append(result, right[0]) 40 | right = right[1:len(right)] 41 | } 42 | } 43 | 44 | return result 45 | } 46 | 47 | func main() { 48 | random := []int{1,73,7,1,72,58,933,574,24,74,52} 49 | fmt.Println("Unsorted: ", random) 50 | random = mergeSort(random) 51 | fmt.Println("Sorted: ", random) 52 | } -------------------------------------------------------------------------------- /solutions/go/multiples-of-3-or-5.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func findMultiples() int { 8 | sum := 0; 9 | for i := 0; i < 1000; i++ { 10 | if i % 3 == 0 || i % 5 == 0 { 11 | sum += i 12 | } 13 | } 14 | return sum 15 | 16 | } 17 | 18 | func main() { 19 | fmt.Println(findMultiples()) 20 | } -------------------------------------------------------------------------------- /solutions/go/shortest-fizz-buzz.go: -------------------------------------------------------------------------------- 1 | package main;import f"fmt";func main(){p:=f.Print;for i:=1;i<101;i++{if i%3<1{p("Fizz")};if i%5<1{p("Buzz")};if i%3>0&&i%5>0{p(i)};p("\n")}} -------------------------------------------------------------------------------- /solutions/go/stack.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "log" 7 | "sync" 8 | ) 9 | 10 | type Stack struct { 11 | lock sync.Mutex // concurrency-safe stack 12 | stack []int 13 | } 14 | 15 | func New() *Stack { 16 | return &Stack{ 17 | lock: sync.Mutex{}, 18 | stack: make([]int, 0), 19 | } 20 | } 21 | 22 | func (s *Stack) Push(n int) { 23 | s.lock.Lock() 24 | defer s.lock.Unlock() 25 | s.stack = append(s.stack, n) 26 | fmt.Printf("pushed %v to the stack \n", n) 27 | } 28 | 29 | func (s *Stack) Pop() (int, error) { 30 | s.lock.Lock() 31 | defer s.lock.Unlock() 32 | 33 | length := len(s.stack) 34 | if length == 0 { 35 | err := errors.New("stack is empty") 36 | return 0, err 37 | } 38 | 39 | // extract the last item in stack 40 | last := s.stack[length - 1] 41 | 42 | // remove the last item in stack 43 | s.stack = s.stack[:length - 1] 44 | 45 | fmt.Printf("removed %v from the stack \n", last) 46 | return last, nil 47 | } 48 | 49 | func main() { 50 | stack := New() 51 | wg := sync.WaitGroup{} 52 | concurrency := 3 53 | 54 | // concurrently push values to the stack 55 | wg.Add(concurrency) 56 | for i := 0; i < concurrency; i++ { 57 | go func(n int) { 58 | stack.Push(n + 10) 59 | wg.Done() 60 | }(i) 61 | } 62 | 63 | // sequentially pop value off the stack 64 | wg.Wait() 65 | for j := 0; j < 3; j++ { 66 | _, err := stack.Pop() 67 | if err != nil { 68 | log.Fatal(err) 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /solutions/java/AnagramDetection.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | 5 | public class AnagramDetection { 6 | 7 | private Map charToPrimeMap; 8 | 9 | public AnagramDetection() { 10 | charToPrimeMap = new HashMap(); 11 | populateCharToPrimeMap(); 12 | } 13 | 14 | private boolean isPrime(int n) { 15 | for (int i = 2; i < Math.ceil(Math.sqrt(n)); ++i) { 16 | if (n % i == 0) { 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | 23 | private int getNthPrime(int n) { 24 | int nthPrime = 2; 25 | while(n > 0) { 26 | if (isPrime(nthPrime)) { 27 | --n; 28 | } 29 | ++nthPrime; 30 | } 31 | return nthPrime; 32 | } 33 | 34 | private void populateCharToPrimeMap() { 35 | for (int i = 0; i < 52; i += 2) { 36 | charToPrimeMap.put((char)('a'+i/2), getNthPrime(i/2)); 37 | charToPrimeMap.put(Character.toUpperCase((char)('a'+i/2)), getNthPrime(i/2+1)); 38 | } 39 | } 40 | 41 | private int hashString(String input) { 42 | int hash = 1; 43 | for (char c: input.toCharArray()) { 44 | hash *= charToPrimeMap.get(c); 45 | } 46 | return hash; 47 | } 48 | 49 | public int detectAnagrams(String original, String anagram) { 50 | char[] originalChars = original.toCharArray(); 51 | int anagramCount = 0; 52 | for (int i = 0; i < original.length() - anagram.length(); ++i) { 53 | String currentSubstring = String.copyValueOf(originalChars, i, anagram.length()); 54 | if (hashString(currentSubstring) == hashString(anagram)) { 55 | ++anagramCount; 56 | } 57 | } 58 | return anagramCount; 59 | } 60 | 61 | public static void main(String[] args) { 62 | AnagramDetection a = new AnagramDetection(); 63 | System.out.println(a.detectAnagrams("AdnBndAndBdaBn", "dAn")); 64 | System.out.println(a.detectAnagrams("AbrAcadAbRa", "cAda")); 65 | } 66 | 67 | 68 | } 69 | -------------------------------------------------------------------------------- /solutions/java/BalancedBrackets.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | import java.util.Stack; 4 | 5 | 6 | public class BalancedBrackets { 7 | 8 | public boolean isBalanced(String s) { 9 | HashMap bracketMap = new HashMap(); 10 | bracketMap.put('{', '}'); 11 | bracketMap.put('[', ']'); 12 | bracketMap.put('(', ')'); 13 | Stack bracketStack = new Stack(); 14 | for (char c: s.toCharArray()) { 15 | if (!bracketStack.isEmpty() && bracketMap.get(bracketStack.peek()) == c) { 16 | bracketStack.pop(); 17 | } else if (bracketMap.containsKey(c)) { 18 | bracketStack.push(c); 19 | } else { 20 | return false; 21 | } 22 | } 23 | return bracketStack.isEmpty(); 24 | } 25 | 26 | public static void main(String[] args) { 27 | BalancedBrackets b = new BalancedBrackets(); 28 | System.out.println(b.isBalanced("()[]{}(([])){[()][]}")); 29 | System.out.println(b.isBalanced("())[]{}")); 30 | System.out.println(b.isBalanced("[(])")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /solutions/java/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Program to create a binary search tree. 3 | * Author: Viveka Aggarwal 4 | */ 5 | 6 | public class BinarySearchTree { 7 | Node root; 8 | public class Node { 9 | Node left; 10 | Node right; 11 | Integer data; 12 | 13 | Node() { 14 | } 15 | 16 | Node(Integer data) { 17 | left = right = null; 18 | this.data = data; 19 | } 20 | } 21 | 22 | BinarySearchTree() { 23 | root = new Node(); 24 | } 25 | 26 | BinarySearchTree(Integer data) { 27 | root = new Node(data); 28 | } 29 | 30 | public void addToTree(Integer data) { 31 | addToTree(root, data); 32 | } 33 | 34 | private void addToTree(Node curr, Integer data) { 35 | if(curr == null) { 36 | curr = new Node(data); 37 | } else if(curr.data.compareTo(data) >= 0) { 38 | if(curr.left == null) 39 | curr.left = new Node(data); 40 | else 41 | addToTree(curr.left, data); 42 | } else { 43 | if(curr.right == null) 44 | curr.right = new Node(data); 45 | else 46 | addToTree(curr.right, data); 47 | } 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return toString(this.root); 53 | } 54 | 55 | String toString(Node curr) { 56 | if(curr == null) 57 | return ""; 58 | return toString(curr.left) + " " + curr.data + " " + toString(curr.right); 59 | } 60 | 61 | public static void main(String[] args) { 62 | BinarySearchTree tree = new BinarySearchTree(1); 63 | tree.addToTree(5); 64 | tree.addToTree(3); 65 | tree.addToTree(89); 66 | tree.addToTree(43); 67 | tree.addToTree(43); 68 | tree.addToTree(67); 69 | 70 | System.out.println(tree.toString()); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /solutions/java/BubbleSort.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, 3 | * compares each pair of adjacent items and swaps them if they are in the wrong order. 4 | * The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. 5 | * Author : Viveka Aggarwal 6 | * Author : Jayesh Chandrapal 7 | */ 8 | 9 | public class BubbleSort { 10 | public static void sort(int[] arr) { 11 | int len = arr.length; 12 | boolean unsorted = true; 13 | 14 | while(unsorted) { 15 | unsorted = false; 16 | 17 | for(int j = 0 ; j < len - 1; j++) { 18 | if(arr[j] > arr[j + 1]) { 19 | swap(j, j + 1, arr); 20 | unsorted = true; 21 | } 22 | } 23 | } 24 | } 25 | 26 | public static void swap(int a, int b, int[] arr) { 27 | int temp = arr[a]; 28 | arr[a] = arr[b]; 29 | arr[b] = temp; 30 | } 31 | 32 | public static void main(String[] a) { 33 | int[] arr = {23, 3, 12, 54, 34, 77, 78, 87, 92, 12}; // unsorted input 34 | //int[] arr = {3,12, 12, 23, 34, 54, 77, 78, 87, 92}; // sorted input 35 | 36 | sort(arr); 37 | 38 | for(int i : arr) { 39 | System.out.print(i + " "); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /solutions/java/ConvertArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | 4 | public class ConvertArray { 5 | 6 | public int getOriginalIndex(int currentIndex, int length) { 7 | return (currentIndex % 3) * length + currentIndex/3; 8 | } 9 | 10 | public int[] convert(int[] input) { 11 | // When array size is less than 3, returns an error. 12 | if (input.length % 3 != 0 ) { 13 | System.out.println("Error! Array cannot be divided into three equal parts"); 14 | return; 15 | } 16 | for (int i = 0; i < input.length; ++i) { 17 | int originalIndex = getOriginalIndex(i, input.length/3); 18 | while (originalIndex < i) { 19 | originalIndex = getOriginalIndex(originalIndex, input.length/3); 20 | } 21 | int temp = input[i]; 22 | input[i] = input[originalIndex]; 23 | input[originalIndex] = temp; 24 | } 25 | return input; 26 | } 27 | 28 | public static void main(String[] args) { 29 | ConvertArray c = new ConvertArray(); 30 | int[] test1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 31 | System.out.println(Arrays.toString(c.convert(test1))); 32 | 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /solutions/java/EvenOccuringElement.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Iterator; 3 | import java.util.Map; 4 | import java.util.Map.Entry; 5 | 6 | public class EvenOccuringElement { 7 | public static void main(String[] args) { 8 | EvenOccuringElement eoe = new EvenOccuringElement(); 9 | int[] input = { 1, 2, 3, 3, 3, 4, 4, 4, 5, 6 }; 10 | Integer evenElement = eoe.getElement(input); 11 | if (evenElement != null) 12 | System.out.println(evenElement); 13 | } 14 | 15 | public Integer getElement(int[] input) { 16 | HashMap counter = new HashMap(); 17 | for (int i = 0; i < input.length; i++) { 18 | if (counter.containsKey(input[i])) 19 | counter.put(input[i], counter.get(input[i]) + 1); 20 | else 21 | counter.put(input[i], 1); 22 | } 23 | Iterator> it = counter.entrySet().iterator(); 24 | while (it.hasNext()) { 25 | Map.Entry pairs = (Entry) it 26 | .next(); 27 | if (((Integer) pairs.getValue() % 2) == 0) 28 | return (Integer) pairs.getKey(); 29 | } 30 | return null; 31 | } 32 | } -------------------------------------------------------------------------------- /solutions/java/Fibonacci.java: -------------------------------------------------------------------------------- 1 | // program to print the fibonacci series 2 | import java.util.Scanner; 3 | 4 | public class Fibonacci { 5 | // computes and displays the value at nth position using dynamic programming 6 | static void printSeries(int n) { 7 | long first = 0,second = 1,third = 0; 8 | 9 | for (int i = 2 ; i < n ; i++) { 10 | third = first + second; 11 | first = second; 12 | second = third; 13 | } 14 | System.out.print("The number at position " +n+" of the fibonacci series is: " +third); 15 | } 16 | 17 | public static void main (String[] a) { 18 | Scanner in = new Scanner(System.in); 19 | System.out.print("Enter the position of the element to displayed in the fibonacci series: "); 20 | int n = in.nextInt(); 21 | System.out.println(); 22 | printSeries(n); 23 | in.close(); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /solutions/java/FindingMissingElement.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class FindingMissingElement { 3 | 4 | /** 5 | *The difference in the sum of the two arrays will give us the desired value 6 | */ 7 | static int find_missing(int a[],int b[]) 8 | { 9 | int sum=0; 10 | for(int i:a) 11 | sum+=i; 12 | for(int i:b) 13 | sum-=i; 14 | return sum; 15 | } 16 | public static void main(String[] args) { 17 | int[] arr={1,3,5,6,2,5,6,8,9,23,45,67,87}; 18 | int[] shuffled_ar={1,2,5,6,8,9,3,45,87,6,67,5}; 19 | System.out.println("The missing value is:"+find_missing(arr,shuffled_ar)); 20 | 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /solutions/java/IntegerDifference.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class IntegerDifference { 4 | public static void main(String[] args) { 5 | IntegerDifference id = new IntegerDifference(); 6 | int[] input = { 1, 1, 3, 3 }; 7 | int diff = 2; 8 | System.out.print(id.getNumberOfElements(input, diff)); 9 | } 10 | 11 | public int getNumberOfElements(int[] input, int diff) { 12 | int numberOfElements = 0; 13 | HashMap sums = new HashMap(); 14 | for (int i = 0; i < input.length; i++) { 15 | int sum = diff + input[i]; 16 | if (sums.containsKey(sum)) 17 | sums.put(sum, sums.get(sum) + 1); 18 | else 19 | sums.put(sum, 1); 20 | } 21 | for (int i = 0; i < input.length; i++) { 22 | if (sums.containsKey(input[i])) { 23 | numberOfElements += sums.get(input[i]); 24 | } 25 | } 26 | return numberOfElements; 27 | } 28 | } -------------------------------------------------------------------------------- /solutions/java/LargestContinuousSum.java: -------------------------------------------------------------------------------- 1 | public class LargestContinuousSum { 2 | public static void main(String[] args) { 3 | LargestContinuousSum lcd = new LargestContinuousSum(); 4 | int[] input = { 1, -1, 2, -5, 10, 15, -10, 5 }; 5 | System.out.print(lcd.getSum(input)); 6 | } 7 | 8 | public int getSum(int[] input) { 9 | int currentMax = input[0]; 10 | int finalMax = input[0]; 11 | for (int i = 1; i < input.length; i++) { 12 | if (currentMax + input[i] > input[i]) 13 | currentMax = currentMax + input[i]; 14 | else 15 | currentMax = input[i]; 16 | 17 | if (currentMax > finalMax) 18 | finalMax = currentMax; 19 | 20 | } 21 | return finalMax; 22 | } 23 | } -------------------------------------------------------------------------------- /solutions/java/LargestPalindrome.java: -------------------------------------------------------------------------------- 1 | public class LargestPalindrome { 2 | public static void main(String[] args) { 3 | LargestPalindrome lp = new LargestPalindrome(); 4 | String input = "I am a red racecar driver"; 5 | System.out.println(lp.getPalindrome(input)); 6 | } 7 | 8 | public String getPalindrome(String input) { 9 | if (input == null || input.length() == 0) 10 | return ""; 11 | if (input.length() == 1) 12 | return input; 13 | 14 | String largestPalindrome = input.charAt(0) + ""; 15 | // Run across the string 16 | for (int i = 1; i < input.length(); i++) { 17 | String curPalindrome = checkSides(input, i, i); 18 | if (curPalindrome.length() > largestPalindrome.length()) 19 | largestPalindrome = curPalindrome; 20 | } 21 | return largestPalindrome; 22 | } 23 | 24 | public String checkSides(String input, int left, int right) { 25 | while (left >= 0 && right < input.length() 26 | && input.charAt(left) == input.charAt(right)) { 27 | left--; 28 | right++; 29 | } 30 | return input.substring(left + 1, right); 31 | } 32 | } -------------------------------------------------------------------------------- /solutions/java/LongestCommonPrefix.java: -------------------------------------------------------------------------------- 1 | public class LongestCommonPrefix { 2 | public static void main(String[] args) { 3 | LongestCommonPrefix lcs = new LongestCommonPrefix(); 4 | String[] input = { "rocket", "rockstar", "rockbottom", "rollingstone"}; 5 | System.out.println(lcs.getSubstring(input)); 6 | } 7 | 8 | public String getSubstring(String[] input) { 9 | String base = input[0]; 10 | for (int i = 0; i < base.length(); i++) { 11 | for (int j = 1; j < input.length; j++) { // Run for all words 12 | String comparer = input[j]; 13 | if (i >= comparer.length() 14 | || comparer.charAt(i) != base.charAt(i)) 15 | return base.substring(0, i); 16 | } 17 | } 18 | return ""; 19 | } 20 | } -------------------------------------------------------------------------------- /solutions/java/NextHighestNumber.java: -------------------------------------------------------------------------------- 1 | public class NextHighestNumber { 2 | /*** 3 | * Convert int into character array. Starting from the end, compare each 4 | * number with all numbers before it, one at a time. At any moment we find a 5 | * number greater than any of its previous numbers, we swap them, and return 6 | * the converted int. 7 | */ 8 | public static void main(String[] args) { 9 | // TODO Auto-generated method stub 10 | NextHighestNumber nhn = new NextHighestNumber(); 11 | Integer next = nhn.getNextHighestNumber(32233); 12 | if (next != null) 13 | System.out.println(next); 14 | } 15 | 16 | public Integer getNextHighestNumber(int n) { 17 | String sNumber = String.valueOf(n); 18 | char[] nums = sNumber.toCharArray(); 19 | for (int i = nums.length - 1; i > 0; i--) { 20 | for (int j = i - 1; j >= 0; j--) 21 | if (Integer.valueOf(nums[i]) > Integer.valueOf(nums[j])) { 22 | char t = nums[i]; 23 | nums[i] = nums[j]; 24 | nums[j] = t; 25 | return Integer.valueOf(new String(nums)); 26 | } 27 | } 28 | return null; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /solutions/java/RemoveDuplicatesFromString.java: -------------------------------------------------------------------------------- 1 | // Program to remove duplicates from a given string 2 | // 1. getUniqueString(String) method uses a boolean array. 3 | // 2. removeDuplicates(String) method uses a hash map. 4 | // Both the methods have O(n) space and time complexity. (n being the string length) 5 | 6 | import java.util.HashMap; 7 | 8 | public class RemoveDuplicatesFromString { 9 | public static void main(String[] args) { 10 | RemoveDuplicatesFromString rsd = new RemoveDuplicatesFromString(); 11 | String input = "Tree Traversal"; 12 | System.out.println("Method 1: " +rsd.getUniqueString(input)); 13 | System.out.println("Method 2: " +rsd.removeDuplicates(input)); 14 | } 15 | 16 | public String getUniqueString(String input) { 17 | boolean[] isUsed = new boolean[256]; 18 | StringBuffer sb = new StringBuffer(); 19 | for (int i = 0; i < input.length(); i++) { 20 | int position = input.charAt(i); 21 | if (!isUsed[position]) { 22 | sb.append(input.charAt(i)); 23 | isUsed[position] = true; 24 | } 25 | } 26 | return sb.toString(); 27 | } 28 | 29 | public String removeDuplicates(String input) { 30 | HashMap map = new HashMap<>(); 31 | StringBuffer sb = new StringBuffer(""); 32 | for (int i = 0; i < input.length(); i++) { 33 | char c = input.charAt(i); 34 | if (!map.containsKey(c)) { 35 | sb.append(c); 36 | map.put(c, 1); 37 | } 38 | } 39 | return sb.toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /solutions/java/ReverseWordsInAString.java: -------------------------------------------------------------------------------- 1 | public class ReverseWordsInAString { 2 | public static void main(String[] args) { 3 | String input = "Interviews are awesome!"; 4 | System.out.println("In: " + input); 5 | 6 | String output = reverseWords(input); 7 | System.out.println("Out: " + output); 8 | } 9 | 10 | private static String reverseWords(String input) { 11 | String[] split = input.split("\\s"); 12 | String output = ""; 13 | for (int i = split.length - 1; i >= 0; i--) { 14 | output += split[i]; 15 | output += " "; 16 | } 17 | return output; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /solutions/java/SearchUnknownLengthArray.java: -------------------------------------------------------------------------------- 1 | public class SearchUnknownLengthArray { 2 | public static void main(String[] args) { 3 | SearchUnknownLengthArray sla = new SearchUnknownLengthArray(); 4 | int[] input = { 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 5, 6, 7, 8, 9, 5 | 9, 9, 9}; 6 | int search = 4; 7 | System.out.println(sla.findElement(input, search)); 8 | } 9 | 10 | /** 11 | * Algorithm: ardendertat.com 12 | * http://www.ardendertat.com/2011/11/21/programming-interview-questions-17-search-unknown-length-array/ 13 | */ 14 | 15 | public int findElement(int[] input, int search) { 16 | int index = 1; 17 | try { 18 | if (input[0] == search) { 19 | return 0; 20 | } 21 | while (index < Integer.MAX_VALUE) { 22 | if (input[index] == search) 23 | return index; 24 | else if (input[index] > search) { 25 | return findBetween(input, search, index); 26 | } 27 | index = (int) Math.pow(2, index); 28 | } 29 | } catch (Exception e) { 30 | return -1; 31 | } 32 | return -1; 33 | } 34 | 35 | public int findBetween(int[] input, int search, int index) { 36 | if (input[index] < search) 37 | return -1; 38 | if (input[index] == search) 39 | return index; 40 | return findBetween(input, search, index - 1); 41 | } 42 | } -------------------------------------------------------------------------------- /solutions/java/StringRotation.java: -------------------------------------------------------------------------------- 1 | public class StringRotation { 2 | 3 | public static void main(String[] args) { 4 | // TODO Auto-generated method stub 5 | StringRotation sr = new StringRotation(); 6 | System.out.println(sr.isRotation("ABCD", "BCDA")); 7 | } 8 | 9 | public boolean isRotation(String one, String two) { 10 | /** 11 | * This code checks if one is a rotation of two The way it works is - 12 | * concatenate the original string with itself, and check if the rotated 13 | * string is contained in it. For example : ABCD is contained in BCDABCDA 14 | */ 15 | return (two + two).contains(one); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /solutions/java/TreeLevelOrderPrint.java: -------------------------------------------------------------------------------- 1 | // Program to create a Binary Search Tree and implement level order traversal. 2 | // Author: Viveka Aggarwal 3 | 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | 7 | public class TreeLevelOrderPrint { 8 | node root; 9 | class node { 10 | Integer value; 11 | node left; 12 | node right; 13 | 14 | node() { 15 | } 16 | 17 | node (Integer value) { 18 | this.value = value; 19 | } 20 | } 21 | 22 | public TreeLevelOrderPrint() { 23 | root = new node(); 24 | } 25 | 26 | public treelevelorderprint(Integer value) { 27 | root = new node(value); 28 | } 29 | 30 | public void addToTree(Integer value) { 31 | if (root.value == null) { 32 | root = new node(value); 33 | } else { 34 | addToTree(value, root); 35 | } 36 | } 37 | 38 | public void addToTree(Integer value, node curr) { 39 | if (value <= curr.value) { 40 | if (curr.left == null) 41 | curr.left = new node(value); 42 | else 43 | addToTree(value, curr.left); 44 | } else { 45 | if (curr.right == null) 46 | curr.right = new node(value); 47 | else 48 | addToTree(value, curr.right); 49 | } 50 | } 51 | 52 | public void levelOrder() { 53 | if (root == null || root.value == null) { 54 | System.out.println(); 55 | System.out.println("Empty tree!!!"); 56 | return; 57 | } 58 | 59 | Queue q = new LinkedList(); 60 | q.add(root); 61 | 62 | while(!q.isEmpty()) { 63 | node curr = q.poll(); 64 | System.out.print(curr.value + " "); 65 | 66 | if(curr.left != null) 67 | q.add(curr.left); 68 | 69 | if(curr.right != null) 70 | q.add(curr.right); 71 | } 72 | } 73 | 74 | public static void main(String[] args) { 75 | TreeLevelOrderPrint tree = new TreeLevelOrderPrint(); 76 | for (int i = 0; i <= 10; i++) { 77 | tree.addToTree(i); 78 | } 79 | tree.addToTree(5); 80 | tree.levelOrder(); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /solutions/javascript/array-pair-sum.js: -------------------------------------------------------------------------------- 1 | module.exports = function arraypairsum (k, array) { 2 | var hash = {}; 3 | var pairs = []; 4 | 5 | // Iterate over the array, tracking the times each number appears. For each 6 | // new number, we calculate the difference to `k` and look up the number of 7 | // times that number has been seen and push those occurances in pairs output. 8 | array.forEach(function (number) { 9 | var diff = k - number; 10 | var len = hash[diff]; 11 | 12 | while (len--) { 13 | pairs.push([diff, number]); 14 | } 15 | 16 | hash[number] = (hash[number] + 1) || 1; 17 | }); 18 | 19 | return pairs; 20 | }; 21 | -------------------------------------------------------------------------------- /solutions/javascript/balanced-brackets.js: -------------------------------------------------------------------------------- 1 | // Use an object to map sets of brackets to their opposites 2 | var brackets = { 3 | '(': ')', 4 | '{': '}', 5 | '[': ']' 6 | }; 7 | 8 | // On each input string, process it using the balance checker 9 | module.exports = function (string) { 10 | var stack = []; 11 | // Process every character on input 12 | for (var i = 0; i < string.length; i++) { 13 | if (brackets[stack[stack.length - 1]] === string[i]) { 14 | stack.pop(); 15 | } else { 16 | stack.push(string[i]); 17 | } 18 | } 19 | 20 | return !stack.length; 21 | }; 22 | -------------------------------------------------------------------------------- /solutions/javascript/binary-search-tree-check.js: -------------------------------------------------------------------------------- 1 | //Method 4 (Using In-Order Traversal) 2 | //Reference: 3 | // http://www.geeksforgeeks.org/a-program-to-check-if-a-binary-tree-is-bst-or-not/ 4 | module.exports = function(bst) { 5 | if (!bst || bst.value == null) { 6 | return false; 7 | } 8 | 9 | var prev; 10 | 11 | return (function isBst(root) { 12 | if (!root) { 13 | return true; 14 | } 15 | 16 | if (!isBst(root.left)) { 17 | return false; 18 | } 19 | 20 | if (prev && prev.value >= root.value) { 21 | return false; 22 | } 23 | 24 | prev = root; 25 | 26 | return isBst(root.right); 27 | 28 | })(bst); 29 | }; -------------------------------------------------------------------------------- /solutions/javascript/bubble-sort.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array, compare) { 2 | // Not an array, empty or array of 1 is already sorted 3 | if (!Array.isArray(array) || array.length < 2) { 4 | return array; 5 | } 6 | 7 | var swap = function (array, first, second) { 8 | var temp = array[first]; 9 | array[first] = array[second]; 10 | array[second] = temp; 11 | return array; 12 | }; 13 | 14 | // Create a compare func if not passed in 15 | if (typeof compare !== 'function') { 16 | compare = function (a, b) { 17 | return a > b ? 1 : -1; 18 | }; 19 | } 20 | 21 | var i, l; 22 | 23 | for (i = 0; i < array.length; i++) { 24 | l = i; 25 | while (l-- && compare(array[l], array[l + 1]) > 0) { 26 | swap(array, l, l + 1); 27 | } 28 | } 29 | 30 | return array; 31 | }; 32 | -------------------------------------------------------------------------------- /solutions/javascript/byte-format.js: -------------------------------------------------------------------------------- 1 | var suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; 2 | 3 | module.exports = function (value, precision) { 4 | var factor = Math.pow(10, precision != null ? precision : 2); 5 | 6 | var suffix = Math.min( 7 | ~~(Math.log(value) / Math.log(1024)), suffixes.length - 1 8 | ); 9 | 10 | var num = Math.ceil(value / Math.pow(1024, suffix) * factor) / factor; 11 | 12 | return num + ' ' + suffixes[suffix]; 13 | }; 14 | -------------------------------------------------------------------------------- /solutions/javascript/combine-two-strings.js: -------------------------------------------------------------------------------- 1 | module.exports = function combineTwoStrings (str1, str2, str3) { 2 | // Simple optimisation to break when impossible. 3 | if ((str1.length + str2.length) !== str3.length) { 4 | return false; 5 | } 6 | 7 | return isCombineTwoStrings(str1, str2, str3); 8 | }; 9 | 10 | function isCombineTwoStrings (str1, str2, str3) { 11 | // No more solutions to find. 12 | if (str3.length === 0) { 13 | return true; 14 | } 15 | 16 | var newStr3 = str3.substr(1); 17 | 18 | // Path for when the first string matches. 19 | if (str1[0] === str3[0]) { 20 | // When both paths are possible, we implement a simple backtracking 21 | // mechanism for when the first was wrong. E.g. `aac`, `aab`, `aaacab`. 22 | if (str2[0] === str3[0]) { 23 | return isCombineTwoStrings(str1.substr(1), str2, newStr3) || 24 | isCombineTwoStrings(str1, str2.substr(1), newStr3); 25 | } 26 | 27 | return isCombineTwoStrings(str1.substr(1), str2, newStr3); 28 | } 29 | 30 | // Path for when the second string matches. 31 | if (str2[0] === str3[0]) { 32 | return isCombineTwoStrings(str1, str2.substr(1), newStr3); 33 | } 34 | 35 | // When neither path is possible, the combination is `false`. 36 | return false; 37 | } 38 | -------------------------------------------------------------------------------- /solutions/javascript/convert-array.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | function getSwapIndex(currentInd, n) { 4 | var swapInd = (currentInd % 3) * n + parseInt(currentInd/3); 5 | while (swapInd < currentInd) { 6 | swapInd = getSwapIndex(swapInd, n); 7 | } 8 | 9 | return swapInd; 10 | } 11 | 12 | function convert(arr) { 13 | var n = parseInt(arr.length / 3); 14 | 15 | for(var i = 0; i < arr.length; i++) { 16 | var swapInd = getSwapIndex(i, n); 17 | 18 | var tmp = arr[i]; 19 | arr[i] = arr[swapInd]; 20 | arr[swapInd] = tmp; 21 | } 22 | 23 | return arr; 24 | } 25 | 26 | module.exports = convert; 27 | -------------------------------------------------------------------------------- /solutions/javascript/csv-parsing.js: -------------------------------------------------------------------------------- 1 | // Please note: This can be accomplished using `JSON.parse('[' + csv + ']')` 2 | module.exports = function (csv) { 3 | var isNumber = false, 4 | isInput = false, 5 | curr = '', 6 | stack = [], 7 | i = 0, 8 | char, 9 | pushStack; 10 | 11 | csv = csv.trim(); 12 | 13 | pushStack = function (input) { 14 | isNumber && (input = +input); 15 | // Resets 16 | curr = ''; 17 | isInput = false; 18 | isNumber = false; 19 | stack.push(input); 20 | }; 21 | 22 | while (char = csv.charAt(i++)) { 23 | if (char === '"') { 24 | isInput = !curr; 25 | } else if (char === ',') { 26 | if (isInput && !isNumber) { 27 | curr += char; 28 | } else { 29 | pushStack(curr); 30 | } 31 | } else if ((isNumber || !curr) && !Number.isNaN(+char)) { 32 | curr += char; 33 | isInput = true; 34 | isNumber = true; 35 | } else { 36 | if (isNumber || !isInput) { throw new Error('Unexpected character'); } 37 | curr += char; 38 | } 39 | } 40 | 41 | // Push the trailing entry 42 | pushStack(curr); 43 | 44 | return stack; 45 | }; 46 | -------------------------------------------------------------------------------- /solutions/javascript/debounce.js: -------------------------------------------------------------------------------- 1 | module.exports = function (fn, delay, execAsap) { 2 | var timeout; // Keep a reference to the timeout outside the function 3 | 4 | return function () { 5 | // Keep the functions execution context and arguments in tact 6 | var that = this, 7 | args = arguments; 8 | 9 | // If we already have a function ready to execute, clear it 10 | // Else if we are allowed to execute immediately, call the function 11 | if (timeout) { 12 | clearTimeout(timeout); 13 | } else if (execAsap) { 14 | fn.apply(that, args); 15 | } 16 | 17 | timeout = setTimeout(function () { 18 | execAsap || fn.apply(that, args); 19 | timeout = null; 20 | }, delay || 100); 21 | }; 22 | }; 23 | -------------------------------------------------------------------------------- /solutions/javascript/even-occuring-element.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array) { 2 | var hash = {}; 3 | 4 | // Loop though the array adding all the elements together in a hash 5 | array.forEach(function (num) { 6 | hash[num] = hash[num] + 1 || 1; 7 | }); 8 | 9 | // Loop through all the keys in the hash, returning the number if we have an 10 | // even number of occurances 11 | for (var i in hash) { 12 | if (!(hash[i] & 1)) { 13 | return Number(i); 14 | } 15 | } 16 | 17 | return false; 18 | }; 19 | -------------------------------------------------------------------------------- /solutions/javascript/factorial.js: -------------------------------------------------------------------------------- 1 | // Recursive function example 2 | exports.recursive = function factorial (number) { 3 | if (number < 2) { return 1; } 4 | 5 | return number * factorial(number - 1); 6 | }; 7 | 8 | // Iterative solution 9 | exports.iterative = function (number) { 10 | var result = 1; 11 | 12 | for (var i = 1; i <= number; i++) { 13 | result *= i; 14 | } 15 | 16 | return result; 17 | }; 18 | 19 | // Iterative using a reverse loop 20 | exports.iterativeReverse = function (number) { 21 | var result = 1; 22 | 23 | while (number) { 24 | result *= number; 25 | number -= 1; 26 | } 27 | 28 | return result; 29 | }; 30 | -------------------------------------------------------------------------------- /solutions/javascript/find-missing-element.js: -------------------------------------------------------------------------------- 1 | // Simple solution using a hash to look up numbers from the second array in the 2 | // first array. When the number doesn't exist in the hash - you know we have 3 | // found the missing number 4 | exports.iterative = function (a, b) { 5 | var hash = {}, i; 6 | 7 | for (i = 0; i < b.length; i++) { 8 | hash[b[i]] = hash[b[i]] + 1 || 1; 9 | } 10 | 11 | for (i = 0; i < a.length; i++) { 12 | if (!hash[a[i]]) { 13 | return a[i]; 14 | } 15 | hash[a[i]] -= 1; 16 | } 17 | }; 18 | 19 | // Bitwise solution using XOR to cancel each of the corresponding numbers out 20 | // with eachother until we end up with a number that isn't cancelled out 21 | exports.bitwise = function (a, b) { 22 | var result = 0; 23 | a.concat(b).forEach(function (num) { 24 | result ^= num; 25 | }); 26 | return result; 27 | }; 28 | 29 | // Maybe the simplest solution, but you can very easily add the two arrays and 30 | // take the result of `b` away from `a` to get the missing number 31 | exports.sum = function (a, b) { 32 | var add = function (a, b) { 33 | return a + b; 34 | }; 35 | 36 | return a.reduce(add) - b.reduce(add); 37 | }; 38 | -------------------------------------------------------------------------------- /solutions/javascript/first-non-repeated-character.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string) { 2 | var checkChar, 3 | prevCharacter; 4 | 5 | checkChar = (function () { 6 | var repeated = false; 7 | 8 | return function (char) { 9 | if (prevCharacter && char === prevCharacter) { 10 | repeated = true; 11 | } 12 | if (prevCharacter && char !== prevCharacter) { 13 | if (!repeated) { return true; } 14 | repeated = false; 15 | } 16 | prevCharacter = char; 17 | // Return false to say it's not been repeated 18 | return false; 19 | }; 20 | })(); 21 | 22 | // Interate one extra time past the last character 23 | for (var i = 0; i <= string.length; i++) { 24 | if (checkChar(string[i])) { return prevCharacter; } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /solutions/javascript/flatten-array.js: -------------------------------------------------------------------------------- 1 | // Create a new flattened array 2 | exports.newArray = function flatten (input) { 3 | var output = []; 4 | 5 | for (var i = 0; i < input.length; i++) { 6 | // Using Array.isArray for new browsers, in older browsers this can be done 7 | // using `Object.prototype.toString.call(input[i]) === '[object Array]'` 8 | if (Array.isArray(input[i])) { 9 | output.push.apply(output, flatten(input[i])); 10 | } else { 11 | output.push(input[i]); 12 | } 13 | } 14 | 15 | return output; 16 | }; 17 | 18 | // In place array flatten 19 | exports.inPlace = function (array) { 20 | var i = 0; 21 | 22 | while (i < array.length) { 23 | if (Array.isArray(array[i])) { 24 | array.splice.apply(array, [i, 1].concat(array[i])); 25 | } else { 26 | i += 1; 27 | } 28 | } 29 | 30 | return array; 31 | }; 32 | 33 | // Flatten array using ES5 reduce method 34 | exports.es5 = function flatten (array) { 35 | return array.reduce(function (arr, val) { 36 | return arr.concat(Array.isArray(val) ? flatten(val) : val); 37 | }, []); 38 | }; 39 | -------------------------------------------------------------------------------- /solutions/javascript/flatten-array/copy.js: -------------------------------------------------------------------------------- 1 | module.exports = function flatten (array) { 2 | var copy = []; 3 | 4 | for (var i = 0; i < array.length; i++) { 5 | if (Array.isArray(array[i])) { 6 | copy.push.apply(copy, flatten(array[i])); 7 | } else { 8 | copy.push(array[i]); 9 | } 10 | } 11 | 12 | return copy; 13 | }; 14 | -------------------------------------------------------------------------------- /solutions/javascript/flatten-array/es5.js: -------------------------------------------------------------------------------- 1 | module.exports = function flatten (array) { 2 | return array.reduce(function (arr, val) { 3 | // Concat is extremely slow which leads to this being the slowest solution. 4 | return arr.concat(Array.isArray(val) ? flatten(val) : val); 5 | }, []); 6 | }; 7 | -------------------------------------------------------------------------------- /solutions/javascript/flatten-array/in-place.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array) { 2 | var i = 0; 3 | 4 | while (i < array.length) { 5 | if (Array.isArray(array[i])) { 6 | array.splice.apply(array, [i, 1].concat(array[i])); 7 | } else { 8 | i += 1; 9 | } 10 | } 11 | 12 | return array; 13 | }; 14 | -------------------------------------------------------------------------------- /solutions/javascript/flatten-array/recursive.js: -------------------------------------------------------------------------------- 1 | var flatten = function (array, result) { 2 | for (var i = 0; i < array.length; i++) { 3 | if (Array.isArray(array[i])) { 4 | flatten(array[i], result); 5 | } else { 6 | result.push(array[i]); 7 | } 8 | } 9 | 10 | return result; 11 | }; 12 | 13 | module.exports = function (array) { 14 | return flatten(array, []); 15 | }; 16 | -------------------------------------------------------------------------------- /solutions/javascript/get-elements-by-class-name.js: -------------------------------------------------------------------------------- 1 | exports.traversal = function (element, className) { 2 | var found = []; 3 | var search = ' ' + className + ' '; 4 | 5 | (function traverse (node) { 6 | // Loop through all the child nodes 7 | for (var i = 0; i < node.childNodes.length; i++) { 8 | var currentNode = node.childNodes[i]; 9 | var currentClass = currentNode.className; 10 | 11 | // Check if the class name exists within the current nodes class 12 | // I believe I learnt of this technique from jQuery source code. 13 | if (currentClass && ~(' ' + currentClass + ' ').indexOf(search)) { 14 | found.push(currentNode); 15 | } 16 | 17 | // If the current node have more child nodes, continue traversing. 18 | currentNode.childNodes && traverse(currentNode); 19 | } 20 | })(element); 21 | 22 | return found; 23 | }; 24 | 25 | exports.regexp = function (element, className) { 26 | // This function takes an easier approach, using a regular expression and 27 | // getting all elements straight up using the asterisk selector. 28 | var found = []; 29 | var regex = new RegExp('(^| )' + className + '($| )'); 30 | var elements = element.getElementsByTagName('*'); 31 | 32 | // Loop through all the elements checking the class names against the 33 | // regular expression - when it suceeds just push it into the output array. 34 | for (var i = 0; i < elements.length; i++) { 35 | if (regex.test(elements[i].className)) { 36 | found.push(elements[i]); 37 | } 38 | } 39 | 40 | return found; 41 | }; 42 | -------------------------------------------------------------------------------- /solutions/javascript/hotel-room.js: -------------------------------------------------------------------------------- 1 | module.exports = function (totalRooms) { 2 | var findDivisors = function (number) { 3 | var divisors = [], 4 | iterator = number; 5 | 6 | while (iterator--) { 7 | if (number % iterator === 0) { 8 | divisors.push(iterator); 9 | } 10 | } 11 | 12 | return divisors; 13 | }; 14 | 15 | // Returns true or false based on whether the number is found in the sum of 16 | // array subsets. 17 | var isSubsetSum = function (number, array) { 18 | var hasSubset = false; 19 | 20 | (function findSubset (total, numbers) { 21 | !hasSubset && (hasSubset = total === number); 22 | 23 | if (hasSubset || total > number) { return; } 24 | 25 | numbers.forEach(function (num, index) { 26 | return findSubset( 27 | total + num, 28 | numbers.slice(0, index).concat(numbers.slice(index + 1)) 29 | ); 30 | }); 31 | })(0, array); 32 | 33 | return hasSubset; 34 | }; 35 | 36 | // Need a simple helper method that returns the sum of an array. 37 | var sumArray = function (array) { 38 | return array.reduce(function (memo, num) { 39 | return memo + num; 40 | }, 0); 41 | }; 42 | 43 | // Find the room using the provided functions. 44 | var divisors; 45 | 46 | for (var room = 0; room <= totalRooms; room++) { 47 | divisors = findDivisors(room); 48 | 49 | // The sum of all the divisors must be greater than the number. 50 | if (sumArray(divisors) > room && !isSubsetSum(room, divisors)) { 51 | return room; 52 | } 53 | } 54 | 55 | return 0; // No room number found. 56 | }; 57 | -------------------------------------------------------------------------------- /solutions/javascript/insertion-sort.js: -------------------------------------------------------------------------------- 1 | // insertion-sort 2 | 'use strict'; 3 | 4 | module.exports = function (array, compare) { 5 | // Not an array, empty or array of 1 is already sorted 6 | if (!Array.isArray(array) || array.length < 2) { 7 | return array; 8 | } 9 | 10 | // Swap elements of the array 11 | var swap = function (array, first, second) { 12 | var temp = array[first]; 13 | array[first] = array[second]; 14 | array[second] = temp; 15 | return array; 16 | }; 17 | 18 | // Create a compare function if one is not passed in 19 | if (typeof compare !== 'function') { 20 | compare = function (a, b) { 21 | return a > b ? 1 : -1; 22 | }; 23 | } 24 | 25 | var i, j; 26 | 27 | /* 28 | * Assume first element is sorted 29 | * Add first unsorted element to sorted array 30 | * Compare new element in sorted array with previous elements 31 | * to determine correct destination index in sorted array 32 | */ 33 | 34 | for (i = 1; i < array.length; i++) { 35 | j = i; 36 | // Make sure we don't walk off the array and compare until sorted 37 | while ((j - 1) >= 0 && compare(array[j], array[j - 1]) < 0) { 38 | swap(array, j, j-1); 39 | j--; 40 | } 41 | } 42 | 43 | return array; 44 | }; 45 | -------------------------------------------------------------------------------- /solutions/javascript/integer-difference.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n, array) { 2 | var hash = {}, 3 | total = 0; 4 | 5 | // Loop through the array once, storing the results in an object for a 6 | // time complexity of O(n) - the naive solution consists of two for loops 7 | // which results in a complexity of O(n^2) 8 | array.forEach(function (number) { 9 | hash[number] = (hash[number] || 0) + 1; 10 | total += (hash[number - n] || 0) + (hash[number + n] || 0); 11 | }); 12 | 13 | return total; 14 | }; 15 | -------------------------------------------------------------------------------- /solutions/javascript/integer-length.js: -------------------------------------------------------------------------------- 1 | module.exports = function (num) { 2 | // There is an extra check here to ensure the number is an integer 3 | return ('' + (num|0)).length; 4 | }; 5 | -------------------------------------------------------------------------------- /solutions/javascript/kth-largest-element-in-array.js: -------------------------------------------------------------------------------- 1 | module.exports = function (k, array) { 2 | // I believe we can store it in a hash to achieve an O(n) complexity 3 | var hash = {}, 4 | count = 0; 5 | 6 | // Loop through each of the array items putting the values as keys in the hash 7 | array.forEach(function (num) { 8 | hash[num] = hash[num] + 1 || 1; 9 | }); 10 | 11 | // Loop through each of the keys in the hash and keep track of the total count 12 | for (var i in hash) { 13 | if (hash.hasOwnProperty(i)) { 14 | // Check if `k` is smaller or equal to the current count plus the current 15 | // hash index, but also greater than the previous count (this will mean it 16 | // is stored in this integer key) 17 | if (k <= count + hash[i] && k > count) { 18 | // Coerce the output back to a number, since that is expected 19 | return +i; 20 | } 21 | // Increment the total count 22 | count += hash[i]; 23 | } 24 | } 25 | 26 | return -1; 27 | }; 28 | -------------------------------------------------------------------------------- /solutions/javascript/largest-continuous-sum.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array) { 2 | if (!array || !array.length) { return; } 3 | 4 | var currentSum, maximumSum; 5 | 6 | // Set the starting sum as the first number 7 | currentSum = maximumSum = array.shift(); 8 | 9 | array.forEach(function (num) { 10 | currentSum = Math.max(currentSum + num, num); 11 | maximumSum = Math.max(currentSum, maximumSum); 12 | }); 13 | 14 | return maximumSum; 15 | }; 16 | -------------------------------------------------------------------------------- /solutions/javascript/largest-palindrome.js: -------------------------------------------------------------------------------- 1 | function isPalindrome (str) { 2 | const mid = Math.floor(str.length / 2) 3 | 4 | for (let i = 0; i < mid; i++) { 5 | if (str[i] !== str[str.length - 1 - i]) { 6 | return false 7 | } 8 | } 9 | 10 | return true 11 | } 12 | 13 | module.exports = function longestPalindrome (str) { 14 | let longest = '' 15 | 16 | for (let i = 0; i < str.length; i++) { 17 | let len = str.length 18 | 19 | while (len > i && len - i > longest.length) { 20 | const substring = str.substring(i, len--) 21 | 22 | if (isPalindrome(substring)) { 23 | longest = substring 24 | } 25 | } 26 | } 27 | 28 | return longest 29 | } 30 | -------------------------------------------------------------------------------- /solutions/javascript/linked-list.js: -------------------------------------------------------------------------------- 1 | var LinkedList = module.exports = function (value) { 2 | this.value = value; 3 | this.prev = this; 4 | this.next = this; 5 | }; 6 | 7 | LinkedList.prototype.appendNode = function (value) { 8 | var node = new LinkedList(value); 9 | node.prev = this; 10 | node.next = this.next; 11 | // Fix the linked list references 12 | this.next = this.next.prev = node; 13 | return node; 14 | }; 15 | 16 | LinkedList.prototype.prependNode = function (value) { 17 | var node = new LinkedList(value); 18 | node.prev = this.prev; 19 | node.next = this; 20 | // Fix the linked list references 21 | this.prev = this.prev.next = node; 22 | return node; 23 | }; 24 | 25 | LinkedList.prototype.removeNode = function () { 26 | // Create a reference around the node to be removed 27 | this.prev.next = this.next; 28 | this.next.prev = this.prev; 29 | // Remove existing references to the current list 30 | this.next = this.prev = this; 31 | return this; 32 | }; 33 | 34 | LinkedList.prototype.containsNode = function (value) { 35 | if (this.value === value) { return true; } 36 | 37 | var node = this.next; 38 | // Loop through the connections until we hit ourselves again 39 | while (node !== this) { 40 | if (node.value === value) { 41 | return true; 42 | } 43 | node = node.next; 44 | } 45 | 46 | return false; 47 | }; 48 | -------------------------------------------------------------------------------- /solutions/javascript/longest-words.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string) { 2 | var length = 0; 3 | // Store the list of longest words in an object to automatically filter 4 | // for duplicates. 5 | var hash = {}; 6 | 7 | // Simplistic splitting on spaces, could improve to trim punctuation as well. 8 | string.split(' ').forEach(function (word) { 9 | // If the word's length is longer than the previous longest, we want to 10 | // update the length and reset the hash back to be empty. 11 | if (word.length > length) { 12 | length = word.length; 13 | hash = {}; 14 | } 15 | 16 | // If the word length is the same as the current longest length, add the 17 | // lowercase version to the hash. Here, we could store the value as some 18 | // arbitrary value since it doesn't matter - if though we needed to return 19 | // the instance of the word we could store how we found the word as the 20 | // value and grab it out when we return. 21 | if (word.length === length) { 22 | return hash[word.toLowerCase()] = true; 23 | } 24 | }); 25 | 26 | // Return an array with all the words. 27 | return Object.keys(hash); 28 | }; 29 | -------------------------------------------------------------------------------- /solutions/javascript/matching-nodes.js: -------------------------------------------------------------------------------- 1 | /* global isMatchingNode */ 2 | 3 | // Make the function accept the node from the first tree and the second tree 4 | // where we need to find the matching node 5 | module.exports = function (node, tree) { 6 | // Easiest way to optimize this is to store the path from the node to the root 7 | // and then we can traverse back down the second tree following the same path 8 | var path = [node], 9 | // Use the child variable to find the matching node in the second tree 10 | child = tree; 11 | 12 | // Loop through all the parent nodes, pushing it into the stack as the pathway 13 | while (node = node.parentNode) { 14 | path.push(node); 15 | } 16 | 17 | // Pop the last thing we pushed onto the path since it'll be the root node 18 | // E.g. It'll be the tree that we are passing in anyway 19 | path.pop(); 20 | 21 | // Once the loop is done, we have the root node and can go back down the tree 22 | while (node = path.pop()) { 23 | for (var i = 0; i < child.childNodes.length; i++) { 24 | // Here we assume we have a function called `isMatchingNode` that returns 25 | // us a boolean in the case of two nodes matching each other 26 | if (isMatchingNode(node, child.childNodes[i])) { 27 | child = child.childNodes[i]; 28 | break; // Break looping over more nodes 29 | } 30 | } 31 | } 32 | 33 | return child; 34 | }; 35 | -------------------------------------------------------------------------------- /solutions/javascript/merge-sort.js: -------------------------------------------------------------------------------- 1 | // Sort the array by breaking it down into smaller chunks 2 | module.exports = function mergeSort (array, compare) { 3 | // If's not an array or an array of just one element, it's already sorted 4 | if (!Array.isArray(array) || array.length < 2) { return array; } 5 | 6 | var length = array.length, 7 | middle = Math.floor(length * 0.5), 8 | left = array.slice(0, middle), 9 | right = array.slice(middle, length); 10 | 11 | // Create a compare func if not passed in 12 | if (typeof compare !== 'function') { 13 | compare = function (a, b) { 14 | return a > b ? 1 : -1; 15 | }; 16 | } 17 | 18 | var merge = function (left, right) { 19 | var result = []; 20 | while (left.length || right.length) { 21 | if (left.length && right.length) { 22 | if (compare(left[0], right[0]) < 1) { 23 | result.push(left.shift()); 24 | } else { 25 | result.push(right.shift()); 26 | } 27 | } else if (left.length) { 28 | result.push(left.shift()); 29 | } else { 30 | result.push(right.shift()); 31 | } 32 | } 33 | return result; 34 | }; 35 | 36 | return merge(mergeSort(left), mergeSort(right)); 37 | }; 38 | -------------------------------------------------------------------------------- /solutions/javascript/missing-number.js: -------------------------------------------------------------------------------- 1 | var add = function (a, b) { 2 | return a + b; 3 | }; 4 | 5 | module.exports = function (array) { 6 | // http://en.wikipedia.org/wiki/Arithmetic_progression#Sum 7 | // Add one to the array length since we are missing a single number, use 1 8 | // as the starting value and the length of the array + 1 as the ending value. 9 | // Then just minus the result of adding all the values together to get our 10 | // missing number. 11 | return ((array.length + 1) * (2 + array.length) / 2) - array.reduce(add, 0); 12 | }; 13 | -------------------------------------------------------------------------------- /solutions/javascript/money-format.js: -------------------------------------------------------------------------------- 1 | module.exports = function (value) { 2 | var remaining = value - ~~value, 3 | string = '' + ~~value, 4 | length = string.length, 5 | places = 0; 6 | 7 | while (--length) { 8 | places += 1; 9 | // At every third position we want to insert a comma 10 | if (places % 3 === 0) { 11 | string = string.substr(0, length) + ',' + string.substr(length); 12 | } 13 | } 14 | 15 | return '$' + string + remaining.toFixed(2).slice(1); 16 | }; 17 | -------------------------------------------------------------------------------- /solutions/javascript/multiples-of-3-and-5.js: -------------------------------------------------------------------------------- 1 | // Accepts a number and an array of multiples 2 | module.exports = function sumOfMultiples (number, multiples) { 3 | return Array.apply(null, new Array(number)).map(function (_, index) { 4 | return index; 5 | }).filter(function (number) { 6 | return multiples.some(function (multiple) { 7 | return number % multiple === 0; 8 | }); 9 | }).reduce(function (memo, number) { 10 | return memo + number; 11 | }); 12 | }; 13 | -------------------------------------------------------------------------------- /solutions/javascript/next-highest-number.js: -------------------------------------------------------------------------------- 1 | module.exports = function (number) { 2 | var numberString = ('' + number); 3 | var length = numberString.length - 1; 4 | var string = ''; 5 | var sorter; 6 | 7 | // Loop in reverse comparing all the digits to the one beside it. 8 | while (length--) { 9 | // Compare the numbers beside each other. 10 | if (numberString.charAt(length) < numberString.charAt(length + 1)) { 11 | // Start the string using the numbers up until the pivot point. 12 | string = numberString.substr(0, length); 13 | // Sort all the numbers after the pivot. 14 | sorter = numberString.substr(length).split('').sort(); 15 | 16 | // Loop through all the numbers and if the next largest number than the 17 | // pivot. 18 | for (var i = 0; i < sorter.length; i++) { 19 | if (sorter[i] > numberString.charAt(length)) { 20 | // Splice the number from it's position and append it to the current 21 | // string. 22 | string += sorter.splice(i, 1)[0]; 23 | break; 24 | } 25 | } 26 | 27 | // Append the rest of the numbers already in ascending order. 28 | string += sorter.join(''); 29 | // Return the string typecast back to a number. 30 | return +string; 31 | } 32 | } 33 | 34 | return number; 35 | }; 36 | -------------------------------------------------------------------------------- /solutions/javascript/next-palindrome-number.js: -------------------------------------------------------------------------------- 1 | module.exports = function nextPalindrome (number) { 2 | var numberString = ('' + number), 3 | numberLength = numberString.length, 4 | oddDigits = numberLength & 1, 5 | leftHalf = numberString.substr(0, ~~(numberLength / 2)), 6 | middleNumber = numberString.charAt(Math.ceil(numberLength / 2)), 7 | increment, newNumber, reverseString; 8 | 9 | reverseString = function (string) { 10 | return string.length > 1 ? string.split('').reverse().join('') : string; 11 | }; 12 | 13 | if (oddDigits) { 14 | increment = Math.pow(10, numberLength / 2); 15 | newNumber = +(leftHalf + middleNumber + reverseString(leftHalf)); 16 | } else { 17 | increment = 1.1 * Math.pow(10, numberLength / 2); 18 | newNumber = +(leftHalf + reverseString(leftHalf)); 19 | } 20 | 21 | if (newNumber > number) { 22 | return newNumber; 23 | } 24 | 25 | if (middleNumber === '9') { 26 | return nextPalindrome(+numberString[0] * Math.pow(10, numberLength)); 27 | } else { 28 | return newNumber + increment; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /solutions/javascript/number-format.js: -------------------------------------------------------------------------------- 1 | exports.iterative = function (number) { 2 | var string = ('' + number).split('.'), 3 | length = string[0].length, 4 | places = 0; 5 | 6 | while (--length) { 7 | places += 1; 8 | // At every third position we want to insert a comma 9 | if (places % 3 === 0) { 10 | string[0] = string[0].substr(0, length) + ',' + string[0].substr(length); 11 | } 12 | } 13 | 14 | return string.join('.'); 15 | }; 16 | 17 | exports.regexp = function (number) { 18 | var string = ('' + number).split('.'); 19 | 20 | string[0] = string[0].replace(/(\d)(?=(?:\d{3})+(?:\.|$))/g, '$1,'); 21 | 22 | return string.join('.'); 23 | }; 24 | -------------------------------------------------------------------------------- /solutions/javascript/odd-occuring-element.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array) { 2 | return array.reduce(function (memo, number) { 3 | return memo ^ number; 4 | }, 0); 5 | }; 6 | -------------------------------------------------------------------------------- /solutions/javascript/once.js: -------------------------------------------------------------------------------- 1 | module.exports = function (fn, times) { 2 | // Set times to one if nothing is passed through and keep track of the 3 | // latest return value to return when we run out of execution times. 4 | var memo; 5 | times = times || 1; 6 | 7 | // Return the function that will be executed. 8 | return function () { 9 | if (!times) { return memo; } 10 | 11 | // Set memo to the result of the function and decrement the number of 12 | // executions. 13 | memo = fn.apply(this, arguments); 14 | times -= 1; 15 | 16 | // If there are no more execution times, set the function to `null` so 17 | // it can be garbage collected and return the memo. 18 | times || (fn = null); 19 | return memo; 20 | }; 21 | }; 22 | -------------------------------------------------------------------------------- /solutions/javascript/prime-number.js: -------------------------------------------------------------------------------- 1 | module.exports = function (n) { 2 | if (n === 2) { return true; } 3 | if (n < 2 || !(n&1)) { return false; } 4 | 5 | for (var i = 3, l = Math.floor(Math.pow(n, 0.5)); i <= l; i += 2) { 6 | if (n % i === 0) { 7 | return false; 8 | } 9 | } 10 | 11 | return true; 12 | }; 13 | -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Checks if two points (p1 and p2) defined by the coordinates 3 | * passed as arguments are either horizontally, vertically or 4 | * diagonally (45°) aligned with one another. 5 | * 6 | * @param {Number} x1 x-coordinate of p1 7 | * @param {Number} y1 y-coordinate of p1 8 | * @param {Number} x2 x-coordinate of p2 9 | * @param {Number} y2 y-coordinate of p2 10 | * @return {Boolean} True if the points are aligned (horz / vert / diag) 11 | */ 12 | module.exports = function (x1, y1, x2, y2) { 13 | 14 | // Validate coordinate values 15 | var validArgs = Array.prototype.slice.call(arguments).every(function (coord) { 16 | return typeof coord === 'number'; 17 | }); 18 | 19 | if (!validArgs) { 20 | throw new Error('All coordinates must numbers'); 21 | } 22 | 23 | return x1 === x2 || y1 === y2 || Math.abs(x1 - x2) === Math.abs(y1 - y2); 24 | 25 | }; 26 | -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king/queen-threatens-king-solution-diagonal-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/javascript/queen-threatens-king/queen-threatens-king-solution-diagonal-1.png -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king/queen-threatens-king-solution-diagonal-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/javascript/queen-threatens-king/queen-threatens-king-solution-diagonal-2.png -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king/queen-threatens-king-solution-horizontal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/javascript/queen-threatens-king/queen-threatens-king-solution-horizontal.png -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king/queen-threatens-king-solution-vertical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blakeembrey/code-problems/c473ac5125b9510f859d2619de08cf4277828e46/solutions/javascript/queen-threatens-king/queen-threatens-king-solution-vertical.png -------------------------------------------------------------------------------- /solutions/javascript/queen-threatens-king/readme.md: -------------------------------------------------------------------------------- 1 | Visualizing the different ways the Queen and the King can be located relative to each other should help solve the problem. As the Queen can only move horizontally, vertically or diagonally (45°) it should be easy to deduce that the King is always threatened by the Queen if they are located in the same row (horizontally aligned) or column (vertically aligned): 2 | 3 | ![The Queen and the King are in the same row](queen-threatens-king-solution-horizontal.png) 4 | ![The Queen and the King are in the same column](queen-threatens-king-solution-vertical.png) 5 | 6 | The somewhat less trivial case is to check if the Queen and the King are in the same diagonal. The problem can be however greatly simplified by only focusing on the part of the board that is covered by the rectangle given by the Queen's and the King's coordinates: 7 | 8 | ![The Queen and the King define a rectangle](queen-threatens-king-solution-diagonal-1.png) 9 | 10 | From here it should become obvious that the Queen and the King will be located on the same diagonal only if the rectangle they form is a square, i.e. Δx == Δy: 11 | 12 | ![The Queen and the King define a rectangle](queen-threatens-king-solution-diagonal-2.png) 13 | 14 | 15 | -------------------------------------------------------------------------------- /solutions/javascript/queue.js: -------------------------------------------------------------------------------- 1 | var Queue = module.exports = function () { 2 | this.head = null; 3 | this.tail = null; 4 | this.length = 0; 5 | }; 6 | 7 | Queue.prototype.enqueue = function (value) { 8 | var node = { 9 | value: value, 10 | next: null 11 | }; 12 | 13 | // If there is currently no head node, set it to the current node. 14 | if (!this.head) { 15 | this.head = node; 16 | } 17 | 18 | // If we have a tail node already, set it's next property to be the current 19 | // node. 20 | if (this.tail) { 21 | this.tail.next = node; 22 | } 23 | 24 | // Update the tail to be the next node. 25 | this.tail = node; 26 | 27 | return this.length += 1; 28 | }; 29 | 30 | Queue.prototype.dequeue = function () { 31 | if (!this.head) { return; } 32 | 33 | var node = this.head; 34 | 35 | // Update the head reference and remove the next node reference from the 36 | // previous head. 37 | this.head = node.next; 38 | node.next = null; 39 | 40 | // Remove the tail node if we have no more head node. 41 | if (!this.head) { this.tail = null; } 42 | 43 | this.length -= 1; 44 | 45 | return node.value; 46 | }; 47 | -------------------------------------------------------------------------------- /solutions/javascript/quick-sort.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function quickSort(input, compare) { 4 | var lesser = [], 5 | greater = [], 6 | pivot; 7 | 8 | if (!Array.isArray(input)) { 9 | throw new Error('Can only sort arrays.'); 10 | } 11 | 12 | var array = input.slice(0); // make a copy of the array 13 | 14 | if (array.length < 2) { 15 | return array; 16 | } 17 | 18 | // Create a compare func if not passed in 19 | if (typeof compare !== 'function') { 20 | compare = function (a, b) { 21 | return a > b ? 1 : -1; 22 | }; 23 | } 24 | 25 | // Get our pivot, this can be random 26 | pivot = array.splice(~~(Math.random() * array.length), 1); 27 | 28 | // Iterate and put vals into either lesser or greater lists compared 29 | // to the pivot 30 | for (var i = 0; i < array.length; i++) { 31 | if (compare(array[i], pivot) < 1) { 32 | lesser.push(array[i]); 33 | } else { 34 | greater.push(array[i]); 35 | } 36 | } 37 | 38 | // Sort lesser and greater lists, concat results 39 | return Array.prototype.concat( 40 | quickSort(lesser, compare), 41 | pivot, 42 | quickSort(greater, compare) 43 | ); 44 | }; 45 | -------------------------------------------------------------------------------- /solutions/javascript/remove-duplicates-from-string.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string) { 2 | var output = '', 3 | hash = {}; 4 | 5 | for (var i = 0; i < string.length; i++) { 6 | if (!hash[string[i]]) { 7 | output += string[i]; 8 | } 9 | hash[string[i]] = true; 10 | } 11 | 12 | return output; 13 | }; 14 | -------------------------------------------------------------------------------- /solutions/javascript/reverse-words-in-string.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string) { 2 | return string.split(/\s+/g).reverse().join(' '); 3 | }; 4 | -------------------------------------------------------------------------------- /solutions/javascript/search-unknown-length-array.js: -------------------------------------------------------------------------------- 1 | module.exports = function searchArray (array, num) { 2 | var index = 0; 3 | 4 | // Increment the index by doubling until we pass the search number. 5 | while (index in array && array[index] <= num) { 6 | if (array[index] === num) { 7 | return index; 8 | } 9 | 10 | // Double the index at which we are seaching. 11 | index = (index * 2) || 1; 12 | } 13 | 14 | // If the index hasn't been incremented beyond the first index, it won't be 15 | // found. 16 | if (index < 2) { return -1; } 17 | 18 | // Track the found index so we can continue to return `-1` down the stack. 19 | var foundIndex = searchArray(array.slice(index / 2, index), num); 20 | 21 | // Return `-1` or old index plus new found index. 22 | return foundIndex > -1 ? (index / 2) + foundIndex : -1; 23 | }; 24 | -------------------------------------------------------------------------------- /solutions/javascript/selection-sort.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array, compare) { 2 | // Not an array, empty or array of 1 is already sorted 3 | if (!Array.isArray(array) || array.length < 2) { 4 | return array; 5 | } 6 | 7 | var swap = function (array, first, second) { 8 | var temp = array[first]; 9 | array[first] = array[second]; 10 | array[second] = temp; 11 | return array; 12 | }; 13 | 14 | // Create a compare func if not passed in 15 | if (typeof compare !== 'function') { 16 | compare = function (a, b) { 17 | return a > b ? 1 : -1; 18 | }; 19 | } 20 | 21 | var min, i, j; 22 | 23 | for (i = 0; i < array.length; i++) { 24 | min = i; 25 | for (j = i + 1; j < array.length; j++) { 26 | if (compare(array[j], array[min]) < 0) { 27 | min = j; 28 | } 29 | } 30 | 31 | swap(array, i, min); 32 | } 33 | 34 | return array; 35 | }; 36 | -------------------------------------------------------------------------------- /solutions/javascript/shortest-fizz-buzz.js: -------------------------------------------------------------------------------- 1 | for(i=0;++i<101;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i) 2 | -------------------------------------------------------------------------------- /solutions/javascript/sorted-array-search.js: -------------------------------------------------------------------------------- 1 | module.exports = function (array, number) { 2 | return (function searchNumber (left, right) { 3 | var center = left + Math.floor((right - left) / 2); 4 | 5 | // If the numbers match, return the center index. 6 | if (array[center] === number) { return center; } 7 | 8 | // If the left position is the same as the center position, return -1. 9 | if (left === center) { return -1; } 10 | 11 | // When the current number is larger than the search input, recurse 12 | // moving the left and right indexes to the search area. 13 | if (array[center] > number) { 14 | return searchNumber(left, center); 15 | } 16 | 17 | // When the current number is smaller move the search parameters to the 18 | // right. 19 | if (array[center] < number) { 20 | return searchNumber(center, right); 21 | } 22 | 23 | return -1; 24 | })(0, array.length); 25 | }; 26 | -------------------------------------------------------------------------------- /solutions/javascript/stack.js: -------------------------------------------------------------------------------- 1 | var Stack = module.exports = function () { 2 | this.head = null; 3 | this.length = 0; 4 | }; 5 | 6 | Stack.prototype.push = function (value) { 7 | var node = { 8 | value: value, 9 | next: null 10 | }; 11 | 12 | if (!this.head) { 13 | this.head = node; 14 | } else { 15 | node.next = this.head; 16 | this.head = node; 17 | } 18 | 19 | return this.length += 1; 20 | }; 21 | 22 | Stack.prototype.pop = function () { 23 | // If there is no head node, return `undefined` 24 | if (!this.head) { return; } 25 | 26 | var node = this.head; 27 | 28 | // Update the head reference and remove the next node reference from the 29 | // previous head. 30 | this.head = node.next; 31 | node.next = null; 32 | 33 | this.length -= 1; 34 | 35 | return node.value; 36 | }; 37 | -------------------------------------------------------------------------------- /solutions/javascript/string-format.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string /* , args */) { 2 | var args = Array.prototype.slice.call(arguments, 1); 3 | 4 | return string.replace(/\{(\d+)\}/g, function (_, arg) { 5 | return arg in args ? args[arg] : _; 6 | }); 7 | }; 8 | -------------------------------------------------------------------------------- /solutions/javascript/string-permutations.js: -------------------------------------------------------------------------------- 1 | module.exports = function (string) { 2 | var result = {}; 3 | 4 | // Using an immediately invoked named function for recursion. 5 | (function makeWord (word, remaining) { 6 | // If there are no more remaining characters, break and set to true 7 | // in the result object. 8 | if (!remaining) { return result[word] = true; } 9 | 10 | // Loop through all the remaining letters and recurse slicing the character 11 | // out of the remaining stack and into the solution word. 12 | for (var i = 0; i < remaining.length; i++) { 13 | makeWord( 14 | word + remaining[i], 15 | remaining.substr(0, i) + remaining.substr(i + 1) 16 | ); 17 | } 18 | })('', string); 19 | 20 | // Using the ES5 Object.keys to grab the all the keys as an array. 21 | return Object.keys(result); 22 | }; 23 | -------------------------------------------------------------------------------- /solutions/javascript/string-rotation.js: -------------------------------------------------------------------------------- 1 | module.exports = function (a, b) { 2 | return a.length === b.length && (a + a).indexOf(b) > -1; 3 | }; 4 | -------------------------------------------------------------------------------- /solutions/javascript/sum-of-array-plus-one.js: -------------------------------------------------------------------------------- 1 | // ES5 method is nice and clean 2 | exports.es5 = function (array) { 3 | return array.reduce(function (memo, num) { 4 | return memo + num; 5 | }, array.length); 6 | }; 7 | 8 | // Without array.reduce method isn't much different 9 | exports.iterative = function (array) { 10 | var result = array.length; 11 | 12 | for (var i = 0; i < array.length; i++) { 13 | result += array[i]; 14 | } 15 | 16 | return result; 17 | }; 18 | -------------------------------------------------------------------------------- /solutions/javascript/throttle.js: -------------------------------------------------------------------------------- 1 | module.exports = function (fn, delay, execAsap) { 2 | var timeout; // Keeps a reference to the timeout inside the returned function 3 | 4 | return function () { 5 | // Continue to pass through the function execution context and arguments 6 | var that = this, 7 | args = arguments; 8 | 9 | // If there is no timeout variable set, proceed to create a new timeout 10 | if (!timeout) { 11 | execAsap && fn.apply(that, args); 12 | 13 | timeout = setTimeout(function () { 14 | execAsap || fn.apply(that, args); 15 | // Remove the old timeout variable so the function can run again 16 | timeout = null; 17 | }, delay || 100); 18 | } 19 | }; 20 | }; 21 | -------------------------------------------------------------------------------- /solutions/javascript/tree-level-order-print.js: -------------------------------------------------------------------------------- 1 | module.exports = function (root) { 2 | // Doing a breadth first search using recursion. 3 | (function walkLevel (children) { 4 | // Create a new queue for the next level. 5 | var queue = [], 6 | output; 7 | 8 | // Use the map function to easily join all the nodes together while pushing 9 | // it's children into the next level queue. 10 | output = children.map(function (node) { 11 | // Assuming the node has children stored in an array. 12 | queue = queue.concat(node.children || []); 13 | return node.value; 14 | }).join(' '); 15 | 16 | // Log the output at each level. 17 | console.log(output); 18 | 19 | // If the queue has values in it, recurse to the next level and walk 20 | // along it. 21 | queue.length && walkLevel(queue); 22 | })([root]); 23 | }; 24 | -------------------------------------------------------------------------------- /solutions/javascript/word-positions.js: -------------------------------------------------------------------------------- 1 | module.exports = function (text) { 2 | var trie = {}, 3 | pos = 0, 4 | active = trie; // Start the active structure as the root trie structure 5 | 6 | // Suffix a space after the text to make life easier 7 | text += ' '; 8 | 9 | // Loop through the input text adding it to the trie structure 10 | for (var i = 0; i < text.length; i++) { 11 | // When the character is a space, restart 12 | if (text[i] === ' ') { 13 | // If the current active doesn't equal the root, set the position 14 | if (active !== trie) { 15 | (active.positions = active.positions || []).push(pos); 16 | } 17 | // Reset the positions and the active part of the data structure 18 | pos = i; 19 | active = trie; 20 | continue; 21 | } 22 | 23 | // Set the next character in the structure up 24 | active[text[i]] = (active[text[i]] || {}); 25 | active = active[text[i]]; 26 | } 27 | 28 | // Return a function that accepts a word and looks it up in the trie structure 29 | return function (word) { 30 | var i = -1, 31 | active = trie; 32 | 33 | while (word[++i]) { 34 | if (!active[word[i]]) { return []; } 35 | active = active[word[i]]; 36 | } 37 | 38 | return active.positions; 39 | }; 40 | }; 41 | -------------------------------------------------------------------------------- /solutions/perl/anagram-detection.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | use Data::Dumper; 3 | my %prime_map = ( 4 | 'a' => 2, 5 | 'b' => 3, 6 | 'c' => 5, 7 | 'd' => 7, 8 | 'e' => 11, 9 | 'f' => 13, 10 | 'g' => 17, 11 | 'h' => 19, 12 | 'i' => 23, 13 | 'j' => 29, 14 | 'k' => 31, 15 | 'l' => 37, 16 | 'm' => 41, 17 | 'n' => 43, 18 | 'o' => 47, 19 | 'p' => 53, 20 | 'q' => 59, 21 | 'r' => 61, 22 | 's' => 67, 23 | 't' => 71, 24 | 'u' => 73, 25 | 'v' => 79, 26 | 'w' => 83, 27 | 'x' => 89, 28 | 'y' => 97, 29 | 'z' => 101, 30 | 'A' => 103, 31 | 'B' => 107, 32 | 'C' => 109, 33 | 'D' => 113, 34 | 'E' => 127, 35 | 'F' => 131, 36 | 'G' => 137, 37 | 'H' => 139, 38 | 'I' => 149, 39 | 'J' => 151, 40 | 'K' => 163, 41 | 'L' => 167, 42 | 'M' => 173, 43 | 'N' => 179, 44 | 'O' => 181, 45 | 'P' => 191, 46 | 'Q' => 193, 47 | 'R' => 197, 48 | 'S' => 199, 49 | 'T' => 211, 50 | 'U' => 223, 51 | 'V' => 227, 52 | 'W' => 229, 53 | 'X' => 233, 54 | 'Y' => 239, 55 | 'Z' => 241 56 | ); 57 | sub compute_checkproduct { 58 | (my $input_string) = @_; 59 | my @characters = split '', $input_string; 60 | my $checkproduct = 1; 61 | foreach $character (@characters) { 62 | $checkproduct *= $prime_map{$character}; 63 | } 64 | $checkproduct; 65 | } 66 | sub detect_anagrams { 67 | (my $haystack, my $needle) = @_; 68 | my $found = 0, $needle_length = length($needle), $haystack_length = length($haystack); 69 | return 0 if ($haystack_length < $needle_length ); 70 | my $needle_checkproduct = compute_checkproduct( $needle ); 71 | for( $i = 0; $i < (length($haystack) - length($needle)); $i++ ) { 72 | my $haystack_slice = substr( $haystack, $i, length($needle) ); 73 | my $haystack_slice_checkproduct = compute_checkproduct( $haystack_slice ); 74 | $found++ if( $haystack_slice_checkproduct == $needle_checkproduct ); 75 | } 76 | $found; 77 | } 78 | print detect_anagrams('AdnBndAndBdaBn', 'dAn'); 79 | print "\n"; 80 | print detect_anagrams('AbrAcadAbRa', 'cAda'); 81 | print "\n"; 82 | -------------------------------------------------------------------------------- /solutions/perl/shortest-fizz-buzz.pl: -------------------------------------------------------------------------------- 1 | print((($_%3?"":"fizz").($_%5?"":"buzz")||"$_")."\n")for 1..100; 2 | -------------------------------------------------------------------------------- /solutions/php/factorial.php: -------------------------------------------------------------------------------- 1 | 0;) { 6 | $remainder = $number % $i; 7 | if (!$remainder) { 8 | $divisors[] = $i; 9 | } 10 | } 11 | return $divisors; 12 | }; 13 | 14 | $getSubsets = function ($divisors) { 15 | $subsets = [[]]; 16 | foreach ($divisors as $division) { 17 | foreach ($subsets as $subset) { 18 | $subsets[] = array_merge([$division], $subset); 19 | } 20 | } 21 | return $subsets; 22 | }; 23 | 24 | $isRoom = function($subsets, $room) { 25 | foreach ($subsets as $subset) { 26 | if (array_sum($subset) == $room) { 27 | return false; 28 | } 29 | } 30 | return true; 31 | }; 32 | 33 | for ($i = 0; ++$i < 101;) { 34 | $divisors = $getDivisors($i); 35 | 36 | $sum = array_sum($divisors); 37 | 38 | if ($sum <= $i) { 39 | continue; 40 | } 41 | 42 | if ($isRoom($getSubsets($divisors), $i)) { 43 | echo $i . "\n"; 44 | exit; 45 | } 46 | } 47 | 48 | echo "?\n"; 49 | 50 | 51 | -------------------------------------------------------------------------------- /solutions/php/integer-length.php: -------------------------------------------------------------------------------- 1 | 8 | -------------------------------------------------------------------------------- /solutions/php/reverse-words-in-string.php: -------------------------------------------------------------------------------- 1 | dependencies_matrix = array(); 11 | } 12 | 13 | public function add_direct($index, $dependencies) 14 | { 15 | $this->dependencies_matrix[$index] = explode(",", $dependencies); 16 | } 17 | 18 | public function dependencies_for($element) 19 | { 20 | //array where dependencies of the element parameter will be stored 21 | $dependents = array(); 22 | //process get element dependencies 23 | $process = $this->dependencies_matrix[$element]; 24 | 25 | while (!empty($process)) { 26 | 27 | //get an item from process 28 | $x = array_shift($process); 29 | 30 | //if x is not element itself, and is not in dependents array 31 | if ($x != $element && !in_array($x, $dependents)) { 32 | 33 | //put in dependents array 34 | $dependents[] = $x; 35 | 36 | //if x exists in dependencies_matrix 37 | if (array_key_exists($x, $this->dependencies_matrix)) { 38 | 39 | //break the next dependencies list in strings 40 | if (gettype($this->dependencies_matrix[$x]) == "array") { 41 | foreach ($this->dependencies_matrix[$x] as $key => $value) { 42 | array_push($process, $value); 43 | } 44 | } 45 | 46 | else { 47 | //put the x line in process 48 | array_push($process, array_values($this->dependencies_matrix[$x])); 49 | } 50 | } 51 | } 52 | } 53 | return $dependents; 54 | } 55 | } //endclass 56 | -------------------------------------------------------------------------------- /solutions/python/anagram-detection.py: -------------------------------------------------------------------------------- 1 | # Simple function that will take a string of latin characters and return a unique hash 2 | def hashString(str): 3 | # Map characters to prime numbers to multiply 4 | charMap = { 5 | 'a': 2, 6 | 'b': 3, 7 | 'c': 5, 8 | 'd': 7, 9 | 'e': 11, 10 | 'f': 13, 11 | 'g': 17, 12 | 'h': 19, 13 | 'i': 23, 14 | 'j': 29, 15 | 'k': 31, 16 | 'l': 37, 17 | 'm': 41, 18 | 'n': 43, 19 | 'o': 47, 20 | 'p': 53, 21 | 'q': 59, 22 | 'r': 61, 23 | 's': 67, 24 | 't': 71, 25 | 'u': 73, 26 | 'v': 79, 27 | 'w': 83, 28 | 'x': 89, 29 | 'y': 97, 30 | 'z': 101, 31 | 'A': 103, 32 | 'B': 107, 33 | 'C': 109, 34 | 'D': 113, 35 | 'E': 127, 36 | 'F': 131, 37 | 'G': 137, 38 | 'H': 139, 39 | 'I': 149, 40 | 'J': 151, 41 | 'K': 163, 42 | 'L': 167, 43 | 'M': 173, 44 | 'N': 179, 45 | 'O': 181, 46 | 'P': 191, 47 | 'Q': 193, 48 | 'R': 197, 49 | 'S': 199, 50 | 'T': 211, 51 | 'U': 223, 52 | 'V': 227, 53 | 'W': 229, 54 | 'X': 233, 55 | 'Y': 239, 56 | 'Z': 241 57 | } 58 | 59 | return reduce(lambda memo, char: memo * charMap[char], list(str), 1); 60 | 61 | def anagramDetection(parent, child): 62 | length = len(child) 63 | anagram = hashString(child) 64 | total = 0 65 | 66 | for i in range(0, len(parent) - length): 67 | if hashString(parent[i: i + length]) == anagram: 68 | total = total + 1 69 | 70 | return total 71 | -------------------------------------------------------------------------------- /solutions/python/balanced-brackets.py: -------------------------------------------------------------------------------- 1 | # Use a dictionary to map sets of brackets to their opposites 2 | brackets = { 3 | '(': ')', 4 | '{': '}', 5 | '[': ']' 6 | } 7 | 8 | # On each input string, process it using the balance checker 9 | def balancedBrackets(string): 10 | stack = [] 11 | # Process every character on input 12 | for char in string: 13 | # Assign an initial value in case the stack is empty 14 | last = 0 15 | # Assign the value of the last element if stack is not empty 16 | if stack: 17 | last = stack[len(stack) - 1] 18 | if stack and last in brackets and brackets[last] == char: 19 | stack.pop() 20 | else: 21 | stack.append(char) 22 | 23 | return not stack 24 | -------------------------------------------------------------------------------- /solutions/python/bubble-sort.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | def bubblesort(lst): 4 | for i in range(0, len(lst) - 1): 5 | for j in range(i + 1, len(lst)): 6 | if lst[i] > lst[j]: 7 | # swap values 8 | lst[i], lst[j] = lst[j], lst[i] 9 | return lst 10 | 11 | class Test(unittest.TestCase): 12 | def testUnsortedSmall(self): 13 | self.assertEqual([1,2,3,4], bubblesort([4,3,2,1])) 14 | 15 | def testSortedSmall(self): 16 | self.assertEqual([1,2,3,4], bubblesort([1,2,3,4])) 17 | 18 | def testUnsortedLarge(self): 19 | self.assertEqual([1,2,3,4,5,6,7,8,9,10], bubblesort([6,3,4,8,7,9,5,10,1,2])) 20 | 21 | def testSortedLarge(self): 22 | self.assertEqual([1,2,3,4,5,6,7,8,9,10], bubblesort([1,2,3,4,5,6,7,8,9,10])) 23 | 24 | def testSingleInput(self): 25 | self.assertEqual([1], bubblesort([1])) 26 | 27 | def testBlankInput(self): 28 | self.assertEqual([], bubblesort([])) 29 | 30 | if __name__ == '__main__': 31 | unittest.main() 32 | -------------------------------------------------------------------------------- /solutions/python/closetsum.py: -------------------------------------------------------------------------------- 1 | # In given A, find three elements whose sum is closest to give value. 2 | #author: Pankaj Kumar 3 | 4 | import sys 5 | def threeSumClosest(A, B): 6 | i , n = 0 , len(A) 7 | A = sorted(A) 8 | diff = sys.maxint 9 | close_sum = 0 10 | while i <= n-3: 11 | j , k = i+1 , n-1 12 | sum = A[i] + A[j] + A[k] 13 | if sum == B: 14 | return sum 15 | if diff > abs(sum - B): 16 | diff += abs(sum-B) 17 | close_sum = sum 18 | if sum < B: 19 | j += 1 20 | else: 21 | k -= 1 22 | i += 1 23 | return close_sum 24 | print threeSumClosest([-1, 2, 1, -4], 1) 25 | -------------------------------------------------------------------------------- /solutions/python/convert-array.py: -------------------------------------------------------------------------------- 1 | """solution to the convert array problem""" 2 | 3 | def f(arr): 4 | """sorts the array by numbers in place using constant extra space""" 5 | 6 | position = 0 7 | for i in xrange(len(arr) / 3): 8 | gap = (len(arr) - position) / 3 9 | arr.insert(position + 1, arr.pop(position + gap * 1)) 10 | arr.insert(position + 2, arr.pop(position + gap * 2)) 11 | position += 3 12 | 13 | return arr 14 | -------------------------------------------------------------------------------- /solutions/python/csv-parsing.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | """solution to the csv parsing problem, handling quotes and numric values""" 4 | 5 | def convert_to_number(value): 6 | """converts the passed over string to a number, if possible""" 7 | 8 | try: 9 | return int(value) 10 | except ValueError: 11 | pass 12 | 13 | return value 14 | 15 | def csv_parsing(csv): 16 | """returns list of string/numeric csv values""" 17 | 18 | count = 0 19 | values = [] 20 | pos = 0 21 | while pos < len(csv): 22 | if csv[pos] == ',': # skip comma from previous iteration 23 | pos += 1 24 | 25 | values.insert(count, '') 26 | while pos < len(csv) and csv[pos] != ',': 27 | if csv[pos] == '"' or csv[pos] == "'": # handle quoted strings 28 | first_quote = pos 29 | pos += 1 30 | 31 | while pos < len(csv) and csv[pos] != csv[first_quote]: 32 | values[count] += csv[pos] 33 | pos += 1 34 | 35 | else: 36 | values[count] += csv[pos] 37 | 38 | pos += 1 39 | 40 | #if the value is numeric, this will convert it. 41 | values[count] = convert_to_number(values[count]) 42 | 43 | count += 1 44 | pos += 1 45 | 46 | return values 47 | -------------------------------------------------------------------------------- /solutions/python/even-occuring-element.py: -------------------------------------------------------------------------------- 1 | def even_occuring_element(arr): 2 | """Returns the even occuring element within a list of integers""" 3 | 4 | dict = {} 5 | for num in arr: 6 | if num in dict: 7 | dict[num] += 1 8 | else: 9 | dict[num] = 1 10 | 11 | for num in dict: 12 | if not dict[num] & 1: # bitwise check for parity. 13 | return num 14 | -------------------------------------------------------------------------------- /solutions/python/factorial.py: -------------------------------------------------------------------------------- 1 | """solutions to the factorial problem""" 2 | 3 | def factorial_iterative(num): 4 | """returns the factorial of num using an iterative method.""" 5 | 6 | factor = 1 7 | 8 | for i in xrange(1, num + 1): 9 | factor *= i 10 | 11 | return factor 12 | 13 | def factorial_reduce(num): 14 | """returns the factorial of num using a reduce (shortest method).""" 15 | 16 | return reduce(lambda x, y: x * y, range(1, num + 1)) 17 | 18 | def factorial_recursive(num): 19 | """returns the factorial of num using a recursive method.""" 20 | 21 | if num == 1: 22 | return 1 23 | 24 | return num * factorial_recursive(num -1) 25 | -------------------------------------------------------------------------------- /solutions/python/fibonacci.py: -------------------------------------------------------------------------------- 1 | """solutions to the fibonacci problem""" 2 | 3 | def fibonacci_iterative(limit): 4 | """fibonacci sequence using an iterative approach.""" 5 | 6 | a, b = 0, 1 7 | for i in xrange(limit): 8 | a, b = b, a + b 9 | 10 | return a 11 | 12 | def fibonacci_recursive(limit): 13 | """fibonacci sequence using a recusive approach.""" 14 | 15 | if limit <= 1: 16 | return limit 17 | 18 | return fibonacci_recursive(limit - 1) + fibonacci_recursive(limit - 2) 19 | 20 | def fibonacci_reduce(limit): 21 | """fibonacci sequence using reduce (shortest option).""" 22 | 23 | return reduce(lambda x, y: x + [x[y] + x[y - 1]], range(1, limit), [0, 1])[-1] 24 | 25 | def fibonacci_comprehension(limit): 26 | """fibonacci sequence using a list comprehension.""" 27 | 28 | sequence = [0, 1] 29 | 30 | [sequence.append(sequence[i] + sequence[i-1]) for i in range(1, limit)] 31 | 32 | return sequence[-1] 33 | -------------------------------------------------------------------------------- /solutions/python/find-missing-element.py: -------------------------------------------------------------------------------- 1 | """finds the missing element in the shuffled list""" 2 | 3 | def difference_set(orig, shuffled): 4 | """finds the missing element using a set.""" 5 | 6 | return set(orig).difference(set(shuffled)).pop() 7 | 8 | def difference_iterative(orig, shuffled): 9 | """finds the missing element by iterating over the list""" 10 | 11 | for x in orig: 12 | if not x in shuffled: 13 | return x 14 | -------------------------------------------------------------------------------- /solutions/python/first-non-repeated-character.py: -------------------------------------------------------------------------------- 1 | """solution for the first-non-repeated-character problem""" 2 | 3 | def first_non_repeated_character(str): 4 | """finds the first character in a string that's not repreated""" 5 | 6 | for i, char in enumerate(str): 7 | if i - 1 >= 0 and char == str[i - 1]: 8 | continue 9 | if i + 1 < len(str) and char == str[i + 1]: 10 | continue 11 | 12 | return char 13 | -------------------------------------------------------------------------------- /solutions/python/flatten-array.py: -------------------------------------------------------------------------------- 1 | """accepts a multi dimensional array and returns a flattened version""" 2 | 3 | def flatten_array(orig): 4 | """returns a new, flattened, list""" 5 | 6 | flattened_list = [] 7 | 8 | for item in orig: 9 | if isinstance(item, list): 10 | flattened_list += flatten_array(item) 11 | else: 12 | flattened_list.append(item) 13 | 14 | return flattened_list 15 | 16 | def flatten_in_place(orig): 17 | """flattens a given list in place""" 18 | 19 | is_flattened = False 20 | 21 | while not is_flattened: # iterating until no more lists are found 22 | 23 | is_flattened = True 24 | for i, item in enumerate(orig): 25 | 26 | if isinstance(item, list): 27 | is_flattened = False 28 | orig = orig[:i] + item + orig[i + 1:] 29 | 30 | return orig 31 | -------------------------------------------------------------------------------- /solutions/python/largest-continuous-sum.py: -------------------------------------------------------------------------------- 1 | """solution to the largest-continuous-sum problem""" 2 | 3 | def largest_continuous_sum(arr): 4 | """returns the highest sum of a continuous sequence in a given list""" 5 | 6 | largest = 0 7 | queue = [] 8 | for num in arr: 9 | if len(queue) > 0 and queue[-1] + 1 != num: 10 | sum = reduce(lambda x, y: x + y, queue) 11 | if largest < sum: 12 | largest = sum 13 | queue = [] 14 | 15 | queue.append(num) 16 | 17 | return largest 18 | -------------------------------------------------------------------------------- /solutions/python/multiples-of-3-and-5.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | from math import ceil 3 | from itertools import combinations 4 | from operator import mul 5 | 6 | # Sum of multiples of 3 or 5 under 1000, simplified: 7 | # print (3 * 333 * 334 / 2) + (5 * 199 * 200 / 2) - (15 * 66 * 67 / 2) 8 | 9 | def getSumOfMultiple(num, limit): 10 | return int((ceil(limit / num) - 1) * ceil(limit / num) * num / 2) 11 | 12 | def getSumOfMultiples(multiples, limit): 13 | result = 0 14 | sign = 1 15 | for i in range(1, len(multiples) + 1): 16 | for x in combinations(multiples, i): 17 | result += sign * getSumOfMultiple(reduce(mul, x, 1), limit) 18 | sign *= -1 19 | return result 20 | 21 | def getSumOfMultiples(): 22 | count=0 23 | for i in range(1000): 24 | if i%3==0 or i%5==0: 25 | count+=i 26 | return count 27 | -------------------------------------------------------------------------------- /solutions/python/once.py: -------------------------------------------------------------------------------- 1 | class once: 2 | def __init__(self, func, times=1): 3 | self.times = int(times) 4 | self.func = func 5 | def __call__(self, *args, **kwargs): 6 | if self.times > 0: 7 | self.times -= 1 8 | return self.func(*args, **kwargs) 9 | -------------------------------------------------------------------------------- /solutions/python/prime-number.py: -------------------------------------------------------------------------------- 1 | from math import sqrt 2 | 3 | def is_prime(n): 4 | if n <= 1: 5 | return False 6 | elif n == 2: 7 | return True 8 | elif n % 2 == 0: 9 | return False 10 | for i in xrange(3, int(sqrt(n))+1, 2): 11 | if n % i == 0: 12 | return False 13 | return True 14 | -------------------------------------------------------------------------------- /solutions/python/quick-sort.py: -------------------------------------------------------------------------------- 1 | from random import randint 2 | 3 | def quickSort(lst): 4 | # List of 0 or 1 items is already sorted 5 | if len(lst) <= 1: 6 | return lst 7 | else: 8 | # Pivot can be chosen randomly 9 | pivotIndex = randint(0, len(lst)-1) 10 | pivot = lst[pivotIndex] 11 | # Elements lower than and greater than pivot 12 | lesser, greater = [], [] 13 | 14 | for index in range(len(lst)): 15 | # Don't do anything if you're at the pivot 16 | if index == pivotIndex: 17 | pass 18 | else: 19 | # Sort elements into < pivot and >= pivot 20 | el = lst[index] 21 | if el < pivot: 22 | lesser.append(el) 23 | else: 24 | greater.append(el) 25 | 26 | # Sort lesser and greater, concatenate results 27 | return quickSort(lesser) + [pivot] + quickSort(greater) 28 | -------------------------------------------------------------------------------- /solutions/python/shortest-fizz-buzz.py: -------------------------------------------------------------------------------- 1 | for i in range(1,101): 2 | o=''if i%3else'Fizz' 3 | if i%5==0:o+='Buzz' 4 | print(o if o else i) 5 | -------------------------------------------------------------------------------- /solutions/python/sum-of-array-plus-one.py: -------------------------------------------------------------------------------- 1 | def plusOneSum(arr): 2 | """returns the sum of the integers after adding 1 to each element""" 3 | return sum(arr)+len(arr) -------------------------------------------------------------------------------- /solutions/python/tree-level-order-print.py: -------------------------------------------------------------------------------- 1 | class Node(object): 2 | def __init__(self, value): 3 | self.value = value 4 | self.left = None 5 | self.right = None 6 | 7 | 8 | def traverse_levelorder(root): 9 | if not root: 10 | return 11 | q = [root, True] # Use True as sentinel for end of row 12 | while len(q) > 0: 13 | node = q.pop(0) 14 | print node.value, 15 | if node.left: 16 | q.append(node.left) 17 | if node.right: 18 | q.append(node.right) 19 | if q[0] is True: # End of row 20 | q.pop(0) 21 | if len(q) > 0: 22 | q.append(True) 23 | print 24 | -------------------------------------------------------------------------------- /solutions/ruby/anagram_detection.rb: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | 3 | # return true if string a and string b are anagrams 4 | def anagram?(a, b) 5 | char_count = proc { |hsh, c| 6 | hsh[c] ||= 0 7 | hsh[c] += 1 8 | hsh 9 | } 10 | if a.each_char.reduce({}, &char_count) == b.each_char.reduce({}, &char_count) 11 | true 12 | else 13 | false 14 | end 15 | end 16 | 17 | 18 | def anagram_detection(parent, child) 19 | x = parent.length - child.length 20 | (0...x).reduce(0) { |c, i| anagram?(parent[i, child.length], child) ? c + 1 : c } 21 | end 22 | 23 | -------------------------------------------------------------------------------- /solutions/ruby/array_pair_sum.rb: -------------------------------------------------------------------------------- 1 | def array_pair_sum(sum, arr) 2 | result = [] 3 | seen = Hash.new(false) 4 | while elem = arr.shift 5 | target = sum - elem 6 | (seen[elem] || seen[target]) ? next : (seen[elem] = true) 7 | result << [elem, target] if arr.include?(target) 8 | end 9 | result 10 | end 11 | 12 | alias :f :array_pair_sum 13 | 14 | puts f(10, [3, 4, 5, 6, 7]).to_s # => [[3, 7], [4, 6]] 15 | puts f(8, [3, 4, 5, 4, 4]).to_s # => [[3, 5], [4, 4]] -------------------------------------------------------------------------------- /solutions/ruby/balanced_brackets.rb: -------------------------------------------------------------------------------- 1 | def balanced_brackets(str) 2 | brackets = { '(' => ')', '{' => '}', '[' => ']' } 3 | 4 | arr = [] 5 | str.each_char do |c| 6 | if brackets[c] then arr << c 7 | else return false if arr.size < 1 || (brackets.key(c) != arr.pop) end 8 | end 9 | arr.empty? 10 | end 11 | -------------------------------------------------------------------------------- /solutions/ruby/bubble_sort.rb: -------------------------------------------------------------------------------- 1 | def bubble_sort(array) 2 | i = 0 3 | while i < array.size - 1 4 | j = i + 1 5 | while j < array.size 6 | array[i], array[j] = array[j], array[i] if array[i] > array[j] 7 | j = j + 1 8 | end 9 | i = i + 1 10 | end 11 | array 12 | end 13 | -------------------------------------------------------------------------------- /solutions/ruby/factorial.rb: -------------------------------------------------------------------------------- 1 | class Integer 2 | # Iterative 3 | def factorial_simple(number) 4 | fat = 1 5 | (2..number).each { |n| fat = fat * n } 6 | fat 7 | end 8 | 9 | # Iterative Reverse Loop 10 | def factorial_reverse(number) 11 | (number - 1).downto(1).each { |i| n *= i } 12 | n 13 | end 14 | 15 | # Recursive - Ternary Operator 16 | def factorial_recursive(number) 17 | (number == 1) ? 1 : (number * factorial_recursive(number-1)) 18 | end 19 | 20 | # The Ruby-iest version, using reduce function 21 | def factorial_reduce(number) 22 | (1..number).reduce(:*) 23 | end 24 | 25 | alias_method :factorial, factorial_reduce 26 | end 27 | -------------------------------------------------------------------------------- /solutions/ruby/fib.rb: -------------------------------------------------------------------------------- 1 | def fib(n) 2 | return 0 if n == 0 3 | return 1 if n == 1 || n == 2 4 | fib(n - 2) + fib(n - 1) 5 | end 6 | -------------------------------------------------------------------------------- /solutions/ruby/fibonacci.rb: -------------------------------------------------------------------------------- 1 | def fib(n) 2 | (0..n).inject([1, 0]) { |(a, b), _| [b, a + b] }[0] 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/flatten_array.rb: -------------------------------------------------------------------------------- 1 | # Note that there is already an implementation in the core ruby library 2 | # http://ruby-doc.org/core-2.2.0/Array.html#method-i-flatten 3 | 4 | def flatten_array(arr = []) 5 | return arr unless arr.is_a? Array 6 | 7 | result = [] 8 | arr.each do |elem| 9 | if elem.is_a? Array 10 | result += flatten_array(elem) 11 | else 12 | result << elem 13 | end 14 | end 15 | 16 | result 17 | end 18 | 19 | flatten_array([]) # => [] 20 | flatten_array([[["a"]]]) # => ["a"] 21 | flatten_array([0, ["damn"], [[["a", "b"]]], 0]) # => [0, "damn", "a", "b", 0] 22 | -------------------------------------------------------------------------------- /solutions/ruby/int_length.rb: -------------------------------------------------------------------------------- 1 | def int_length(num) 2 | return 1 if num.abs < 10 3 | 1 + int_length(num / 10) 4 | end 5 | -------------------------------------------------------------------------------- /solutions/ruby/longest_words.rb: -------------------------------------------------------------------------------- 1 | def longest_words string 2 | # splitting the string into words and removing duplicates from the array 3 | words = string.downcase.split.uniq 4 | # finding the maximum value in the array based on length 5 | max = words.max_by { |word| word.length } 6 | # selecting all the words with the same length as the max value 7 | words = words.select { |word| word.length == max.length } 8 | end -------------------------------------------------------------------------------- /solutions/ruby/merge_sort.rb: -------------------------------------------------------------------------------- 1 | def merge_sort(a) 2 | return a if a.size <= 1 3 | l, r = split_array(a) 4 | combine(merge_sort(l), merge_sort(r)) 5 | end 6 | 7 | def split_array(a) 8 | mid = (a.size / 2).round 9 | [a.take(mid), a.drop(mid)] 10 | end 11 | 12 | def combine(a, b) 13 | return b.empty? ? a : b if a.empty? || b.empty? 14 | smallest = a.first <= b.first ? a.shift : b.shift 15 | combine(a, b).unshift(smallest) 16 | end 17 | 18 | # a = [6, 23, 53, 1, 2, 5, 62, 61, 33, 21, 14, 6, 23].shuffle 19 | # p merge_sort(a) 20 | -------------------------------------------------------------------------------- /solutions/ruby/multiples_of_three_and_five.rb: -------------------------------------------------------------------------------- 1 | def multiples_of_three_and_five 2 | (1...1000).to_a.select { |num| num % 3 == 0 || num % 5 == 0 }.reduce(&:+) 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/odd_occuring_element.rb: -------------------------------------------------------------------------------- 1 | def odd_occuring_element(array) 2 | array.uniq.each do |num| 3 | return num if array.count(num) % 2 != 0 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /solutions/ruby/prime_number.rb: -------------------------------------------------------------------------------- 1 | # Iterative version 2 | 3 | def is_prime? number 4 | return 0 if (number == 0 or number == 1) 5 | 6 | for d in (2...number) do 7 | return 0 if (number % d == 0) 8 | end 9 | 10 | return 1 11 | end 12 | 13 | #built-in version 14 | 15 | require 'mathn' #mathematical library 16 | 17 | def is_prime2? number 18 | (Prime.prime? number)? 1:0 19 | end 20 | -------------------------------------------------------------------------------- /solutions/ruby/quick_sort.rb: -------------------------------------------------------------------------------- 1 | # NOTE: Ruby has a built in sorting method, `sort!`, that actually uses a quick-sort process; this just demonstrates an implementation. 2 | 3 | def quick_sort(array) # quick sort algorithm 4 | array.quick_sort! 5 | end 6 | 7 | def self.quick_sort!(array) 8 | process(array, 0, array.size - 1) 9 | end 10 | 11 | private 12 | 13 | def self.process(array, left, right) 14 | if left < right 15 | pivot = partition(array, left, right) 16 | process(array, left, pivot - 1) 17 | process(array, pivot + 1, right) 18 | end 19 | array 20 | end 21 | 22 | def self.partition(array, left, right) 23 | x = array[right] 24 | i = left - 1 25 | (left..right - 1).each do |j| 26 | if array[j] <= x 27 | i += 1 28 | array[i], array[j] = array[j], array[i] 29 | end 30 | end 31 | array[i + 1], array[right] = array[right], array[i + 1] 32 | i + 1 33 | end 34 | -------------------------------------------------------------------------------- /solutions/ruby/remove_duplicates.rb: -------------------------------------------------------------------------------- 1 | def remove_duplicates(str) 2 | str.split("").uniq.join 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/reverse_words.rb: -------------------------------------------------------------------------------- 1 | def reverse_words(array) 2 | array.split(" ").reverse.join(" ") 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/selection_sort.rb: -------------------------------------------------------------------------------- 1 | def selection_sort(array) 2 | 0.upto(array.size - 1) do |i| 3 | min = i 4 | (i + 1).upto(array.size - 1) { |j| min = j if (array[j] <=> array[min]) == -1 } 5 | array[i], array[min] = array[min], array[i] 6 | end 7 | array 8 | end 9 | -------------------------------------------------------------------------------- /solutions/ruby/shortest_fizz_buzz.rb: -------------------------------------------------------------------------------- 1 | def shortest_fizz_buzz 2 | (1..100).to_a.each{|i|puts i%3==0&&i%5==0 ? "FizzBuzz": i%3==0 ? "Fizz": i%5==0 ? "Buzz": i} 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/sum_of_array_plus_one.rb: -------------------------------------------------------------------------------- 1 | def sum_of_array_plus_one array 2 | array.inject(:+) + array.count 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/sum_plus_one.rb: -------------------------------------------------------------------------------- 1 | def sum_plus_one(array) 2 | array.reduce(&:+) + array.size 3 | end 4 | -------------------------------------------------------------------------------- /solutions/ruby/word_position.rb: -------------------------------------------------------------------------------- 1 | def word_position(text, word) 2 | text = text.split(" ") 3 | positions = [] 4 | text.each_index do |i| 5 | positions << i if text[i] == word 6 | end 7 | return positions 8 | end 9 | -------------------------------------------------------------------------------- /solutions/scala/AnagramDetection.scala: -------------------------------------------------------------------------------- 1 | import scala.collection.immutable.HashMap 2 | 3 | object AnagramDetection { 4 | def apply(parent: String, child: String): Int = 5 | parent.length - child.length match { 6 | case 0 => if (isAnagram(parent, child)) 1 else 0 7 | case x: Int if x < 0 => 0 8 | case _ => 9 | (if (isAnagram(parent.take(child.length), child)) 1 else 0) + 10 | AnagramDetection(parent.drop(1), child) 11 | } 12 | 13 | def isAnagram(stra: String, strb: String) = { 14 | val hma = (HashMap[Char, Int]() /: stra) { (hmap, c) => 15 | hmap updated (c, hmap.getOrElse(c, 0)) 16 | } 17 | val hmb = (HashMap[Char, Int]() /: strb) { (hmap, c) => 18 | hmap updated (c, hmap.getOrElse(c, 0)) 19 | } 20 | 21 | if (hma == hmb) true else false 22 | } 23 | } 24 | 25 | // println(AnagramDetection("AbrAcadAbRa", "cAda")) => 2 26 | // println(AnagramDetection("AdnBndAndBdaBn", "dAn")) => 4 27 | -------------------------------------------------------------------------------- /solutions/scala/Factorial.scala: -------------------------------------------------------------------------------- 1 | object Factorial { 2 | def apply(n: Int) = { 3 | if (n < 1) throw new IllegalArgumentException 4 | (1 to n) reduce { (mem, e) => mem * e } 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /solutions/scala/Fibonacci.scala: -------------------------------------------------------------------------------- 1 | object Fibonacci { 2 | // accept a pos, starting with 1 3 | def apply(pos: Int) = { 4 | if (pos < 0) throw new IllegalArgumentException 5 | (1 until pos).foldLeft((0, 1))((t, _) => (t._2, t._1 + t._2))._1 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/cpp/common.cpp: -------------------------------------------------------------------------------- 1 | #include "common.hpp" 2 | 3 | std::deque> eventLog; 4 | int nextId; 5 | -------------------------------------------------------------------------------- /tests/cpp/common.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_HPP_INCLUDED 2 | #define COMMON_HPP_INCLUDED 3 | 4 | #include // std::deque 5 | #include // std::tuple 6 | 7 | enum class Event { 8 | DefaultConstructorCalled, 9 | CopyConstructorCalled, 10 | MoveConstructorCalled, 11 | AssignmentOperatorCalled, 12 | DestructorCalled 13 | }; 14 | 15 | extern std::deque> eventLog; 16 | extern int nextId; 17 | 18 | inline const int getId() { 19 | return nextId++; 20 | }; 21 | 22 | inline void log(int id, Event e) { 23 | eventLog.push_back(std::make_tuple(id, e)); 24 | }; 25 | 26 | class eventLogger { 27 | private: 28 | int id; 29 | public: 30 | eventLogger() : id{getId()} { 31 | log(id, Event::DefaultConstructorCalled); 32 | }; 33 | eventLogger(eventLogger const& t) : id{getId()} { 34 | log(id, Event::CopyConstructorCalled); 35 | }; 36 | eventLogger(eventLogger&& t) : id{getId()} { 37 | log(id, Event::MoveConstructorCalled); 38 | }; 39 | ~eventLogger() { 40 | log(id, Event::DestructorCalled); 41 | }; 42 | eventLogger& operator=(eventLogger const& t) { 43 | log(id, Event::AssignmentOperatorCalled); 44 | return *this; 45 | }; 46 | }; 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /tests/cpp/spiral.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | 3 | #include "../../solutions/cpp/spiral.hpp" 4 | 5 | TEST(SpiralTest, BasicTest) { 6 | typedef std::vector v; 7 | EXPECT_EQ(v({ 13, 8, 7, 12, 17, 18, 19, 14, 9, 4, 3, 2, 1, 6, 11, 16, 8 | 21, 22, 23, 24, 25, 20, 15, 10, 5 }), spiral(5, 5, 3, 3)); 9 | EXPECT_EQ(v({ 2, 1, 5, 6, 7, 3, 8, 4 }), spiral(2, 4, 1, 2)); 10 | } 11 | -------------------------------------------------------------------------------- /tests/javascript/anagram-detection.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var anagramDetection = require('../../solutions/javascript/anagram-detection'); 3 | 4 | describe('Anagram Detection', function () { 5 | it('should detect no change in the characters', function () { 6 | expect(anagramDetection('abc', 'abc')).to.equal(1); 7 | }); 8 | 9 | it('should detect anagrams of itself', function () { 10 | expect(anagramDetection('aab', 'baa')).to.equal(1); 11 | }); 12 | 13 | it('should detect child anagrams', function () { 14 | expect(anagramDetection('AbrAcadAbRa', 'cAda')).to.equal(2); 15 | expect(anagramDetection('AdnBndAndBdaBn', 'dAn')).to.equal(4); 16 | }); 17 | 18 | it('should not fail with a larger child than parent string', function () { 19 | expect(anagramDetection('test', 'testing')).to.equal(0); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /tests/javascript/array-pair-sum.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var arrayPairSum = require('../../solutions/javascript/array-pair-sum'); 3 | 4 | describe('Array Pair Sum', function () { 5 | it('should find pairs that equal the expected sum', function () { 6 | expect(arrayPairSum(10, [3, 4, 5, 6, 7])).to.eql([[4, 6], [3, 7]]); 7 | }); 8 | 9 | it('should not output duplicate results', function () { 10 | expect(arrayPairSum(8, [3, 4, 5, 4, 4])).to.eql([[3, 5], [4, 4], [4, 4], [4, 4]]); 11 | }); 12 | 13 | it('should work not find any matches', function () { 14 | expect(arrayPairSum(10, [3, 5, 6, 8])).to.eql([]); 15 | }) 16 | }); 17 | -------------------------------------------------------------------------------- /tests/javascript/balanced-brackets.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var balancedBrackets = require('../../solutions/javascript/balanced-brackets'); 3 | 4 | 5 | describe('Balanced brackets', function () { 6 | 7 | it('should detect pairing brackets', function () { 8 | expect(balancedBrackets('()[]{}')).to.be.true; 9 | }); 10 | it('should detect nested brackets', function () { 11 | expect(balancedBrackets('(([])){[()][]}')).to.be.true; 12 | }); 13 | 14 | it('should detect non pairing brackets ', function () { 15 | expect(balancedBrackets('())[]{}')).to.be.false; 16 | }); 17 | 18 | it('should detect wrong nesting ', function () { 19 | expect(balancedBrackets('[(])')).to.be.false; 20 | }); 21 | 22 | }); 23 | -------------------------------------------------------------------------------- /tests/javascript/bubble-sort.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var bubble_sort = require('../../solutions/javascript/insertion-sort'); 3 | 4 | describe('it tests bubble sort function', function(){ 5 | var sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 6 | var array = [9, 8, 7, 6, 5, 4, 3, 2, 1]; 7 | 8 | it("sort array: " + sorted, function(){ 9 | array = bubble_sort(array); 10 | expect(array).deep.equal(sorted); 11 | }) 12 | 13 | }); 14 | -------------------------------------------------------------------------------- /tests/javascript/byte-format.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var byteFormat = require('../../solutions/javascript/byte-format'); 3 | 4 | describe('Byte format', function() { 5 | it('should show B format', function() { 6 | expect(byteFormat(1022)).to.equal('1022 B'); 7 | }); 8 | 9 | it('should show KB format', function() { 10 | expect(byteFormat(10221)).to.equal('9.99 KB'); 11 | }); 12 | 13 | it('should show KB format rounded to 3 digital', function() { 14 | expect(byteFormat(10221, 3)).to.equal('9.982 KB'); 15 | }); 16 | 17 | it('should show MB format', function() { 18 | expect(byteFormat(1022932324)).to.equal('975.55 MB'); 19 | }); 20 | 21 | it('should show GB format', function() { 22 | expect(byteFormat(1022932123237)).to.equal('952.68 GB'); 23 | }); 24 | 25 | it('should show TB format', function() { 26 | expect(byteFormat(1022932453333234)).to.equal('930.36 TB'); 27 | }); 28 | 29 | it('should show PB format', function() { 30 | expect(byteFormat(1022932453333234444)).to.equal('908.55 PB'); 31 | }); 32 | 33 | it('should show EB format', function() { 34 | expect(byteFormat(1022932453333234444324)).to.equal('887.26 EB'); 35 | }); 36 | 37 | it('should show ZB format', function() { 38 | expect(byteFormat(1022932453333234444324454)).to.equal('866.46 ZB'); 39 | }); 40 | 41 | it('should show YB format', function() { 42 | expect(byteFormat(10243245333323444432445431)).to.equal('8.48 YB'); 43 | }); 44 | 45 | it('should show YB format when number larger than 1024YB', function() { 46 | var result = byteFormat(232932453333234444324454333424324); 47 | 48 | expect(result).to.equal('192677209.44 YB'); 49 | }); 50 | 51 | it('should not show any decimals', function () { 52 | expect(byteFormat(9999, 0)).to.equal('10 KB'); 53 | }); 54 | }); 55 | -------------------------------------------------------------------------------- /tests/javascript/combine-two-strings.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var combineTwoStrings = require('../../solutions/javascript/combine-two-strings'); 3 | 4 | describe('it tests combine two strings function', function(){ 5 | var str1 = 'abc'; 6 | var str2 = 'def'; 7 | var str3valid = 'dabecf'; 8 | var str3invalid = 'dabfce'; 9 | 10 | it("it tests that f('" + str1 + "," +str2 + ",'" +str3valid + "')' is valid " , function(){ 11 | var result = combineTwoStrings(str1, str2, str3valid); 12 | expect(result).to.be.true; 13 | }); 14 | 15 | it("it tests that f('" + str1 + "," +str2 + ",'" +str3invalid + "')' is invalid " , function(){ 16 | var result = combineTwoStrings(str1, str2, str3invalid); 17 | expect(result).to.be.false; 18 | }); 19 | 20 | }); 21 | -------------------------------------------------------------------------------- /tests/javascript/convert-array.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var convert = require('../../solutions/javascript/convert-array'); 3 | 4 | describe('Convert Array', function() { 5 | it('should convert an array', function() { 6 | var arr = [ 7 | 'a1', 'a2', 'a3', 'a4', 'a5', 8 | 'b1', 'b2', 'b3', 'b4', 'b5', 9 | 'c1', 'c2', 'c3', 'c4', 'c5' 10 | ]; 11 | 12 | var expectedArr = [ 13 | 'a1', 'b1', 'c1', 14 | 'a2', 'b2', 'c2', 15 | 'a3', 'b3', 'c3', 16 | 'a4', 'b4', 'c4', 17 | 'a5', 'b5', 'c5' 18 | ]; 19 | 20 | convert(arr); 21 | 22 | for(var i = 0; i < arr.length; i++) { 23 | assert.equal(arr[i], expectedArr[i]); 24 | } 25 | }) 26 | }) 27 | -------------------------------------------------------------------------------- /tests/javascript/factorial.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var factorial = require('../../solutions/javascript/factorial'); 3 | 4 | describe('factorial', function () { 5 | // We have multiple factorial solutions to test, so loop through them 6 | Object.keys(factorial).forEach(function (name) { 7 | // Set the value in the object as the current solution to work against 8 | var solution = factorial[name]; 9 | // Use the name as part of the assertion name 10 | it(name + ' solution returns factorials', function () { 11 | // Bunch of simple assertions we know to be true 12 | assert.equal(solution(1), 1); 13 | assert.equal(solution(2), 2); 14 | assert.equal(solution(3), 6); 15 | assert.equal(solution(4), 24); 16 | assert.equal(solution(5), 120); 17 | assert.equal(solution(6), 720); 18 | assert.equal(solution(7), 5040); 19 | assert.equal(solution(8), 40320); 20 | assert.equal(solution(9), 362880); 21 | assert.equal(solution(10), 3628800); 22 | assert.equal(solution(11), 39916800); 23 | assert.equal(solution(12), 479001600); 24 | assert.equal(solution(13), 6227020800); 25 | assert.equal(solution(14), 87178291200); 26 | assert.equal(solution(15), 1307674368000); 27 | assert.equal(solution(16), 20922789888000); 28 | assert.equal(solution(17), 355687428096000); 29 | assert.equal(solution(18), 6402373705728000); 30 | assert.equal(solution(19), 121645100408832000); 31 | assert.equal(solution(20), 2432902008176640000); 32 | }); 33 | }); 34 | }); 35 | -------------------------------------------------------------------------------- /tests/javascript/flatten-array.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var fs = require('fs'); 4 | var expect = require('chai').expect; 5 | var dirname = __dirname + '/../../solutions/javascript/flatten-array'; 6 | 7 | // Load all the solutions from the test directory. 8 | var solutions = fs.readdirSync(dirname).filter(function (file) { 9 | return /\.js$/.test(file); 10 | }).map(function (file) { 11 | return [file.replace(/\.js$/, ''), require(dirname + '/' + file)]; 12 | }); 13 | 14 | describe('flatten-array', function () { 15 | solutions.forEach(function (solution) { 16 | var flatten = solution[1]; 17 | 18 | describe(solution[0], function () { 19 | it('should flatten an array', function () { 20 | expect(flatten([1, [2, [3], 2], 1])).to.deep.equal([1, 2, 3, 2, 1]); 21 | }); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /tests/javascript/insertion-sort.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var insertion = require('../../solutions/javascript/insertion-sort'); 3 | 4 | describe('sort functions ->', function(){ 5 | var sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 6 | var array = [9, 8, 7, 6, 5, 4, 3, 2, 1]; 7 | 8 | it("sort array: " + sorted, function(){ 9 | array = insertion(array); 10 | assert.deepEqual(array, sorted); 11 | }) 12 | }) 13 | 14 | -------------------------------------------------------------------------------- /tests/javascript/largest-continuous-sum.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var solution = require('../../solutions/javascript/largest-continuous-sum'); 3 | 4 | describe('largest continuous sum', function () { 5 | it('should be able to get the largest sum', function () { 6 | assert.equal(solution([1, 2, 3]), 6); 7 | assert.equal(solution([2, 4, -1, 4, 5]), 14); 8 | assert.equal(solution([10, -5, 15, -20, 5]), 20); 9 | }); 10 | 11 | it('should work with negative numbers', function () { 12 | assert.equal(solution([-1, -2, -3]), -1); 13 | }); 14 | 15 | it('should return nothing when given nothing', function () { 16 | assert.equal(solution(), undefined); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /tests/javascript/next-highest-number.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var nextHighest = require('../../solutions/javascript/next-highest-number'); 3 | 4 | describe('next highest number', function () { 5 | it('should return the next highest number', function () { 6 | assert.equal(nextHighest(1524), 1542); 7 | assert.equal(nextHighest(1542), 2145); 8 | assert.equal(nextHighest(1543), 3145); 9 | assert.equal(nextHighest(1544), 4145); 10 | assert.equal(nextHighest(63254), 63425); 11 | assert.equal(nextHighest(63542), 64235); 12 | assert.equal(nextHighest(1112), 1121); 13 | assert.equal(nextHighest(1243), 1324); 14 | assert.equal(nextHighest(348932), 349238); 15 | assert.equal(nextHighest(15895), 15958); 16 | }); 17 | 18 | it('should work when there isn\'t a higher number', function () { 19 | assert.equal(nextHighest(98765), 98765); 20 | assert.equal(nextHighest(11111), 11111); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /tests/javascript/prime-number.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var primeNumber = require('../../solutions/javascript/prime-number'); 3 | 4 | describe('prime number', function () { 5 | it('should return true if the number is a prime number', function () { 6 | assert.equal(primeNumber(2), true); 7 | assert.equal(primeNumber(859), true); 8 | assert.equal(primeNumber(983), true); 9 | assert.equal(primeNumber(283), true); 10 | }); 11 | 12 | it('should return false if the number is not prime', function () { 13 | assert.equal(primeNumber(324), false); 14 | assert.equal(primeNumber(-124), false); 15 | assert.equal(primeNumber(89325), false); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /tests/javascript/queen-threatens-king.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var threatens = require('../../solutions/javascript/queen-threatens-king'); 3 | 4 | describe('queen-threatens-king', function () { 5 | 6 | it('should throw if called with invalid parameters', function () { 7 | var coordinates = [ 0, 0, null, 0 ]; 8 | var threatensWithInvalidParams = Function.prototype.bind.apply(threatens, coordinates); 9 | expect(threatensWithInvalidParams).to.throw(Error); 10 | }); 11 | 12 | it('the queen should threaten the king (in the same row)', function () { 13 | expect(threatens(0, 0, 0, 1)).to.be.true; 14 | }); 15 | 16 | it('the queen should threaten the king (in the same column)', function () { 17 | expect(threatens(0, 0, 1, 0)).to.be.true; 18 | }); 19 | 20 | it('the queen should threaten the king (in the same diagonal)', function () { 21 | expect(threatens(0, 0, 1, 1)).to.be.true; 22 | }); 23 | 24 | it('the queen should not threaten the king', function () { 25 | expect(threatens(0, 0, 1, 2)).to.be.false; 26 | }); 27 | 28 | }); 29 | -------------------------------------------------------------------------------- /tests/javascript/quick-sort.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | var quickSort = require('../../solutions/javascript/quick-sort'); 5 | 6 | describe('Quick Sort', function(){ 7 | 8 | it('throws error if input not an array', function() { 9 | 10 | try { 11 | quickSort(null); 12 | } catch (err) { 13 | return; 14 | } 15 | 16 | assert(false, 'Expected exception to be thrown.'); 17 | }); 18 | 19 | it('handles empty list', function() { 20 | var results = quickSort([]); 21 | assert(Array.isArray(results), 'Expected result to be array'); 22 | assert(results.length === 0, 'Expected array to be empty'); 23 | }); 24 | 25 | it('handles list of one', function() { 26 | var results = quickSort([1]); 27 | assert.deepEqual(results, [1], 'Expected arrays to match'); 28 | }); 29 | 30 | it('sorts an array of numbers', function() { 31 | var unsorted = [9, 8, 7, 6, 5, 4, 3, 2, 1]; 32 | var sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 33 | var results = quickSort(unsorted); 34 | assert.deepEqual(results, sorted, 'Expected arrays to match'); 35 | }); 36 | 37 | it('sorts an array of words', function() { 38 | var words = ['carrot','beta','apple']; 39 | var sorted = ['apple','beta','carrot']; 40 | var results = quickSort(words); 41 | 42 | assert.deepEqual(results, sorted, 'Expected arrays to match'); 43 | }); 44 | 45 | it('leaves input array intact', function() { 46 | var words = ['apple','beta','carrot']; 47 | quickSort(words); 48 | assert(words.length === 3); 49 | }); 50 | 51 | 52 | }); 53 | -------------------------------------------------------------------------------- /tests/javascript/search-unknown-length-array.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var searchArray = require( 3 | '../../solutions/javascript/search-unknown-length-array' 4 | ); 5 | 6 | describe('Search Unknown Length Array', function () { 7 | it('should find the number', function () { 8 | expect(searchArray([3, 5, 6, 7, 8], 3)).to.equal(0); 9 | expect(searchArray([-8, -6, -5, -1, 5, 12, 99], 99)).to.equal(6); 10 | expect(searchArray([1, 4, 6, 7, 9, 12, 34, 47, 53, 65], 12)).to.equal(5); 11 | }); 12 | 13 | it('should fail to find the number', function () { 14 | expect(searchArray([1, 2, 3], 4)).to.equal(-1); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /tests/javascript/sorted-array-search.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var sortedSearch = require('../../solutions/javascript/sorted-array-search'); 3 | 4 | describe('Sorted Array Search', function () { 5 | it('should be able to find the number', function () { 6 | expect(sortedSearch([2, 5, 6, 9, 23, 45, 85, 102], 9)).to.equal(3); 7 | expect(sortedSearch([2, 8, 9, 10, 53, 63, 73, 83], 83)).to.equal(7); 8 | expect(sortedSearch([1, 4, 6, 7, 9, 11, 45, 98, 99], 1)).to.equal(0); 9 | }); 10 | 11 | it('should find negative numbers', function () { 12 | expect(sortedSearch([-11, -10, -7, -5, -3, -2, 5, 8], -10)).to.equal(1); 13 | expect(sortedSearch([-99, -67, -45, -33, -10, -9, -8], -8)).to.equal(6); 14 | }); 15 | 16 | it('should fail to find the number', function () { 17 | expect(sortedSearch([3, 4, 7, 9, 44], 23)).to.equal(-1); 18 | }); 19 | }); 20 | -------------------------------------------------------------------------------- /tests/javascript/word-positions.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var wordPositions = require('../../solutions/javascript/word-positions'); 3 | 4 | describe('word positions', function () { 5 | it('should return an array of word positions', function () { 6 | assert.deepEqual(wordPositions('test')('test'), [0]); 7 | assert.deepEqual(wordPositions('test test test')('test'), [0, 4, 9]); 8 | assert.deepEqual(wordPositions('find a word in some sentence')('in'), [11]); 9 | }); 10 | 11 | it('should return an empty array when not found', function () { 12 | assert.deepEqual(wordPositions('it does not exist')('test'), []); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /tests/php/IntegerLengthTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(1, IntegerLength(1)); 11 | $this->assertEquals(2, IntegerLength(22)); 12 | $this->assertEquals(3, IntegerLength(333)); 13 | $this->assertEquals(10, IntegerLength(1234567890)); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/php/factorialTest.php: -------------------------------------------------------------------------------- 1 | verifyFactorial("factorial"); 11 | } 12 | 13 | public function test_factorialReverse() 14 | { 15 | $this->verifyFactorial("factorialReverse"); 16 | } 17 | 18 | public function test_factorialRecursive() 19 | { 20 | $this->verifyFactorial("factorialRecursive"); 21 | } 22 | 23 | private function verifyFactorial($fn) 24 | { 25 | $testDataMap = [ 26 | ["input" => 1, "expect" => 1], 27 | ["input" => 2, "expect" => 2], 28 | ["input" => 3, "expect" => 6], 29 | ["input" => 5, "expect" => 120], 30 | ["input" => 20, "expect" => 2432902008176640000], 31 | ]; 32 | 33 | foreach ($testDataMap as $data) { 34 | $this->assertEquals($data["expect"], call_user_func($fn, $data["input"])); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/php/reverseWordsInStringTest.php: -------------------------------------------------------------------------------- 1 | assertEquals("awesome! are Interviews", reverseWordsInStrings("Interviews are awesome!")); 11 | $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree")); 12 | $this->assertEquals("degree CS", reverseWordsInStrings("CS degree")); 13 | $this->assertEquals("degree CS", reverseWordsInStrings("CS degree ")); 14 | $this->assertEquals("degree CS", reverseWordsInStrings(" CS degree ")); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/php/sumOfArrayPlusOneTest.php: -------------------------------------------------------------------------------- 1 | assertEquals(14, SumOfArrayPlusOne([1, 2, 3, 4])); 11 | 12 | } 13 | } 14 | --------------------------------------------------------------------------------