├── ReverseString ├── Javascript │ └── Reverse.js ├── README.md └── C │ └── Reverse.c ├── ReverseEachIndex ├── Javascript │ └── ReverseEachIndex.js └── README.md ├── Factorial ├── Javascript │ └── Factorial.js ├── README.md └── C++ │ ├── FactorialRecursion.cpp │ └── FactorialIteration.cpp ├── FizzBuzz ├── Python │ └── fizzbuzz.py ├── Javascript │ └── fizzbuzz.js ├── README.md ├── C++ │ ├── fizzbuzz.cpp │ └── FizzbuzzFunction.cpp └── Java │ └── FizzBuzz.java ├── Templates └── README-Template.md ├── StringRotation ├── README.md └── C# │ └── StringRotation.cs ├── .github └── ISSUE_TEMPLATE │ ├── Feature_request.md │ └── Bug_report.md ├── AnagramCheck ├── README.md ├── Python │ └── anagramCheck.py ├── Javascript │ └── AnagramCheck.js └── Java │ └── AnagramCheck.java ├── SquareRoot ├── C++ │ └── SquareRoot.cpp ├── README.md └── Python │ └── SquareRoot.py ├── IsPrime ├── README.md └── Python │ └── isPrime.py ├── Searching Algorithms ├── RecursiveLinearSearch │ ├── README.md │ └── Python │ │ └── RecursiveLinearSearch.py └── BinarySearch │ ├── README.md │ └── C++ │ └── BinarySearch.cpp ├── DuplicateInList ├── README.md └── C# │ └── DuplicateInList.cs ├── CaesarCipher ├── README.md └── Python │ └── CaesarCipher.py ├── LICENSE ├── README.md └── CONTRIBUTING.md /ReverseString/Javascript/Reverse.js: -------------------------------------------------------------------------------- 1 | let reverseString = string => console.log(string.split("").reverse().join("")); 2 | reverseString("Bet you won't reverse me.."); 3 | -------------------------------------------------------------------------------- /ReverseEachIndex/Javascript/ReverseEachIndex.js: -------------------------------------------------------------------------------- 1 | let array = ["These", "are", "the", "values", "that", "will", "be", "reversed."]; 2 | let reverse = arr => arr.forEach(item => console.log(item.split("").reverse().join(""))); 3 | reverse(array); 4 | -------------------------------------------------------------------------------- /Factorial/Javascript/Factorial.js: -------------------------------------------------------------------------------- 1 | function factorial(num) { 2 | if (num === 0 || num === 1) 3 | return 1; 4 | return num * factorial(num-1); 5 | } 6 | 7 | // Tests 8 | console.log(factorial(0)); // => 1 9 | console.log(factorial(5)); // => 120 10 | -------------------------------------------------------------------------------- /FizzBuzz/Python/fizzbuzz.py: -------------------------------------------------------------------------------- 1 | # Written by JGFM4 2 | # Last update: 2018-02-21 3 | 4 | for i in range(1,101): 5 | if i % 15 == 0: 6 | print("FizzBuzz") 7 | elif i % 3 == 0: 8 | print("Fizz") 9 | elif i % 5 == 0: 10 | print("Buzz") 11 | else: 12 | print(i) 13 | -------------------------------------------------------------------------------- /FizzBuzz/Javascript/fizzbuzz.js: -------------------------------------------------------------------------------- 1 | for(let i = 1; i <= 100; i++){ 2 | if(i % 15 === 0){ 3 | console.log("FIZZBUZZ"); 4 | } 5 | else if(i % 3 === 0){ 6 | console.log("FIZZ") 7 | } 8 | else if(i % 5 === 0){ 9 | console.log("BUZZ") 10 | } 11 | else{ 12 | console.log(i); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Templates/README-Template.md: -------------------------------------------------------------------------------- 1 | # Name of Algorithm 2 | **Problem Definition:** 3 | Definition of the algorithm/problem to be solved should be provided here 4 | 5 | --- 6 | **Examples:** 7 | * Examples provided as bulletpoints or a blocks of code. 8 | 9 | --- 10 | **Notes:** 11 | * Notes added as bulletpoints 12 | -------------------------------------------------------------------------------- /StringRotation/README.md: -------------------------------------------------------------------------------- 1 | # String Rotation 2 | **Problem Definition:** 3 | Write a function that when given a string and a non-negative number, it returns a string rotated by the given amount. 4 | 5 | --- 6 | 7 | **Examples:** 8 | * `"abcd", 1 => "bcda"` 9 | * `"abcd", 6 => "cdab"` 10 | 11 | --- 12 | 13 | **Notes:** 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an algorithm 4 | 5 | --- 6 | 7 | **Algorithm** 8 | Name: `Name` 9 | 10 | Short Description: `A clear and concise description of the algorithm.` 11 | 12 | Programming language: `language` 13 | 14 | Reference Page: `If available, provide a URL to the webpage describing the algorithm` 15 | -------------------------------------------------------------------------------- /Factorial/README.md: -------------------------------------------------------------------------------- 1 | # Factorial 2 | **Problem Definition:** 3 | Write a function that takes a number as an argument and return its factorial value. 4 | 5 | --- 6 | **Examples:** 7 | * `Factorial(5) = 5 * 4 * 3 * 2 * 1 = 120` 8 | * `Factorial(3) = 3 * 2 * 1 = 6` 9 | * `Factorial(1) = 1`; 10 | 11 | --- 12 | **Notes:** 13 | * Common ways of solving this problem are through Iteration or Recursion. 14 | -------------------------------------------------------------------------------- /FizzBuzz/README.md: -------------------------------------------------------------------------------- 1 | # FizzBuzz 2 | **Problem Definition:** 3 | This is a classic algorithm. 4 | Task: 5 | 1. Print out numbers from 1-100. 6 | 2. Print out "FIZZ" for every 3rd number. 7 | 3. Print out "BUZZ" for every 5th number. 8 | 4. Print out "FIZZBUZZ" for every 15th number. 9 | 10 | --- 11 | 12 | **Examples:** 13 | * Output up to 5: 14 | ``` 15 | 1 16 | 2 17 | FIZZ 18 | 4 19 | BUZZ 20 | ``` 21 | 22 | --- 23 | 24 | **Notes:** -------------------------------------------------------------------------------- /Factorial/C++/FactorialRecursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorialRec(int); 5 | 6 | int main(){ 7 | cout << "rec 0! = " << factorialRec(0) << endl; // => 1 8 | cout << "rec 5! = " << factorialRec(5) << endl; // => 120 9 | return 0; 10 | } 11 | 12 | // recursively version 13 | int factorialRec(int num){ 14 | if(num == 0){ 15 | return 1; 16 | } 17 | else{ 18 | return num * factorial_rec(num - 1); 19 | } 20 | } -------------------------------------------------------------------------------- /AnagramCheck/README.md: -------------------------------------------------------------------------------- 1 | # Anagram Check 2 | **Problem Definition:** 3 | Write a function which takes two strings and see if they are anagrams (the letters of one input can rearrange into the other input) of each other by returning a boolean. 4 | 5 | --- 6 | 7 | **Examples:** 8 | 9 | ``` 10 | anagramCheck("Tom Marvolo Riddle", "I am Lord Voldemort") = true 11 | anagramCheck("Hello", "olleH") = true 12 | anagramCheck("Hi", "hello") = false 13 | ``` 14 | 15 | --- 16 | 17 | **Notes:** -------------------------------------------------------------------------------- /Factorial/C++/FactorialIteration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int factorialIter(int); 5 | 6 | // Test program 7 | int main(){ 8 | cout << "iter 0! = " << factorialIter(0) << endl; // => 1 9 | cout << "iter 5! = " << factorialIter(5) << endl; // => 120 10 | return 0; 11 | } 12 | 13 | // iterative version 14 | int factorialIter(int num){ 15 | int total = 1; 16 | for(int i = num; i > 1; i--){ 17 | total *= i; 18 | } 19 | return total; 20 | } 21 | -------------------------------------------------------------------------------- /SquareRoot/C++/SquareRoot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | double sqrt(double); 5 | 6 | int main(){ 7 | // This should output 12 8 | cout << sqrt(144) << endl; 9 | return 0; 10 | } 11 | 12 | double sqrt(double num){ 13 | // r = root 14 | // a = change to root 15 | double a = 0; 16 | double r = 1.0; 17 | while (((r * r) - num) > 0.0001 || ((r * r) - num) < -0.0001){ 18 | r -= a; 19 | a = ((r * r) - num) / num; 20 | } 21 | return r; 22 | } 23 | -------------------------------------------------------------------------------- /FizzBuzz/C++/fizzbuzz.cpp: -------------------------------------------------------------------------------- 1 | // Copyright © 2018 Austin Tice. All rights reserved. 2 | #include 3 | 4 | int main() { 5 | for(int i = 1; i <= 100; i++){ 6 | if(i % 15 == 0){ 7 | std::cout << "FIZZBUZZ" << std::endl; 8 | } 9 | else if(i % 3 == 0){ 10 | std::cout << "FIZZ" << std::endl; 11 | } 12 | else if(i % 5 == 0){ 13 | std::cout << "BUZZ" << std::endl; 14 | } 15 | else{ 16 | std::cout << i << std::endl; 17 | } 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /AnagramCheck/Python/anagramCheck.py: -------------------------------------------------------------------------------- 1 | def anagramCheck(a, b): 2 | assert isinstance(a, str) and isinstance(b, str) 3 | a = a.replace(" ", "").lower() 4 | b = b.replace(" ", "").lower() 5 | return sorted(a) == sorted(b) 6 | 7 | def main(): 8 | print(anagramCheck("Tom Marvolo Riddle", "I am Lord Voldemort")) # => True 9 | print(anagramCheck("Hello", "olleH")) # => True 10 | print(anagramCheck("Hi", "hello")) # => False 11 | 12 | if __name__ == "__main__": 13 | main() 14 | -------------------------------------------------------------------------------- /FizzBuzz/Java/FizzBuzz.java: -------------------------------------------------------------------------------- 1 | // Written by JGFM4 2 | // Last update: 2018-02-20 3 | 4 | public class FizzBuzz{ 5 | public static void main(String [] args){ 6 | 7 | for (int i = 1; i <= 100; i++){ 8 | if (i % 15 == 0){ 9 | System.out.println("FizzBuzz"); 10 | 11 | } else if (i % 3 == 0){ 12 | System.out.println("Fizz"); 13 | 14 | } else if (i % 5 == 0){ 15 | System.out.println("Buzz"); 16 | 17 | } else { 18 | System.out.println(i); 19 | } 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /SquareRoot/README.md: -------------------------------------------------------------------------------- 1 | # Square Root 2 | **Problem Definition:** 3 | Create a custom function that can calculate the square root of any number in an efficient manner 4 | 5 | --- 6 | **Examples:** 7 | * `sqrt(144) = 12` 8 | * `sqrt(432.3) = 20.79` 9 | 10 | --- 11 | **Notes:** 12 | * *Reminder:* The square root of a number is the number that will equal it's square when multiplied by itself.`Ex: 2 * 2 = 4` 13 | * Remember a square root can be denoted as raising a number to its 1/2 power `9^(1/2) = 3` 14 | -------------------------------------------------------------------------------- /AnagramCheck/Javascript/AnagramCheck.js: -------------------------------------------------------------------------------- 1 | function anagramCheck(x, y){ 2 | str = x.toLowerCase() 3 | .replace(/\s/g, "") 4 | .split("") 5 | .sort() 6 | .join(""); 7 | str2 = y.toLowerCase() 8 | .replace(/\s/g, "") 9 | .split("") 10 | .sort() 11 | .join(""); 12 | return str === str2; 13 | } 14 | 15 | console.log(anagramCheck("Hello World", "dlroW olleH")); // => true 16 | console.log(anagramCheck("Imtkheing'", "I'm the king")); // => true 17 | console.log(anagramCheck("test", "tesd")); // => false 18 | -------------------------------------------------------------------------------- /ReverseEachIndex/README.md: -------------------------------------------------------------------------------- 1 | # Reverse Each Index 2 | **Problem Definition:** 3 | In this algorithm you are tasked with writing a function that takes in an array as an argument; write code to reverse the order 4 | of each of the indexes contents. 5 | 6 | --- 7 | **Examples:** 8 | * Array that is given: `["Reverse", "my", "order"]` 9 | * Expected Output: `["esreveR", "ym", "redro"]` 10 | 11 | --- 12 | **Notes:** 13 | * Try to make this as efficient as possible by using built in functions that are native to your specific programming language! 14 | -------------------------------------------------------------------------------- /FizzBuzz/C++/FizzbuzzFunction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // prototype 4 | void fizz_buzz(int); 5 | 6 | int main() 7 | { 8 | for (int i = 1; i <= 100; i++) 9 | fizz_buzz(i); 10 | return 0; 11 | } 12 | 13 | void fizz_buzz(int n) 14 | { 15 | if (n % 15 == 0) 16 | { 17 | std::cout << "FIZZBUZZ" << std::endl; 18 | } 19 | else if (n % 3 == 0) 20 | { 21 | std::cout << "FIZZ" << std::endl; 22 | } 23 | else if (n % 5 == 0) 24 | { 25 | std::cout << "BUZZ" << std::endl; 26 | } 27 | else 28 | { 29 | std::cout << n << std::endl; 30 | } 31 | } -------------------------------------------------------------------------------- /SquareRoot/Python/SquareRoot.py: -------------------------------------------------------------------------------- 1 | def squareRoot(num): 2 | """[summary] 3 | Uses the heron-procedure. 4 | 5 | Arguments: 6 | num {[float]} -- [radikant] 7 | 8 | Returns: 9 | [float] -- [result] 10 | """ 11 | xn = num / 10.0 # init step 12 | EPSILON = 0.001 # precision 13 | while abs(xn**2 - num) > EPSILON: 14 | xn = 0.5 * (xn + (num/xn)) 15 | return xn 16 | 17 | def main(): 18 | # Test => 2.000 19 | print("sqrt(2)={0}".format(squareRoot(4.0))) 20 | 21 | if __name__ == "__main__": 22 | main() -------------------------------------------------------------------------------- /IsPrime/README.md: -------------------------------------------------------------------------------- 1 | # IsPrime 2 | **Problem Definition:** 3 | Given some integer, write an isPrime(num) function to determine whether or not the given number is prime or not. The function should return True or False. 4 | 5 | --- 6 | **Examples:** 7 | * isPrime(2) == True 8 | * isPrime(9) == False 9 | * isPrime(41) == True 10 | 11 | --- 12 | **Notes:** 13 | * The formal definition of a prime number: A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole numbers that can be divided evenly into another number. 14 | -------------------------------------------------------------------------------- /ReverseString/README.md: -------------------------------------------------------------------------------- 1 | # Reverse String 2 | **Problem Definition:** 3 | Write a function that takes in a string as a parameter, and manipulate that string so that it displays in reverse order. 4 | 5 | --- 6 | **Examples:** 7 | * String that is given: ` "Reverse my letters" ` 8 | * Expected output: ` "srettel ym esreveR" ` 9 | 10 | --- 11 | **Notes:** 12 | This simple, yet elegant problem, is a common interview question in which interviewers want to see if you understand the very basics of 13 | a language. It's important you understand this basic algorithm if you are just beginning in the tech industry. 14 | -------------------------------------------------------------------------------- /Searching Algorithms/RecursiveLinearSearch/README.md: -------------------------------------------------------------------------------- 1 | # Recursive Linear Search 2 | **Problem Definition:** 3 | Define a recursive function that, as input, takes an array and a search value and returns the index where it's first found in the array. Return -1 if the value is not found. 4 | 5 | --- 6 | **Examples:** 7 | * `arr = [1, 1, 2, 3, 5, 8]` 8 | * recSearch(arr, 8, ..your choice of params..) => 5 9 | * recSearch(arr, 1, ..your choice of params..) => 0 10 | 11 | --- 12 | **Notes:** 13 | In this program seek to find the most optimal recursive algorithm to do this search. Choosing meaningful parameters is a good start. 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Algorithm** 8 | * Name: `AlgorithmName.ext` 9 | * Language: `ProgrammingLanguage` 10 | 11 | **Describe the bug** 12 | A clear and concise description of what the bug is. 13 | 14 | **Problematic inputs and expected output** 15 | 1. `input -> output` 16 | 2. `input2 -> output2` 17 | 3. ... 18 | 19 | **Error messages** 20 | If applicable, add the error message. This include compilation and run-time errors. 21 | 22 | **Additional context** 23 | Add any other context about the problem here. 24 | -------------------------------------------------------------------------------- /ReverseString/C/Reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | char * reverseString(char []); 6 | 7 | // main : for testing 8 | int main(void){ 9 | char * testStr = "Reverse my letters"; 10 | char * result = reverseString(testStr); 11 | printf("%s\n", result); // => "srettel ym esreveR" 12 | free(result); 13 | return 0; 14 | } 15 | 16 | char * reverseString(char s[]){ 17 | int SIZE = strlen(s); 18 | int i; 19 | int j; 20 | char * ans = (char*) malloc(SIZE); 21 | for(i = SIZE-1, j = 0; i >= 0; i--, j++){ 22 | ans[j] = s[i]; 23 | } 24 | return ans; 25 | } -------------------------------------------------------------------------------- /StringRotation/C#/StringRotation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StringRotation{ 4 | class Program{ 5 | static void Main(string[] args){ 6 | Console.WriteLine(Rotate("abcd", 6)); 7 | } 8 | 9 | static string Rotate(string str, int shiftAmount){ 10 | // Get remainder in case shiftAmount > str.Length 11 | shiftAmount = shiftAmount % str.Length; 12 | 13 | // Separate the string into two halves at the index shiftAmount 14 | str = str.Substring(shiftAmount) + str.Substring(0, shiftAmount); 15 | 16 | return str; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Searching Algorithms/RecursiveLinearSearch/Python/RecursiveLinearSearch.py: -------------------------------------------------------------------------------- 1 | # Recursive function to search x in arr[l..r] 2 | def recSearch(arr, front, back, num): 3 | if back < front: 4 | return -1 5 | if arr[front] == num: 6 | return front 7 | if arr[back] == num: 8 | return back 9 | return recSearch(arr, front+1, back-1, num) 10 | 11 | # Driver Code 12 | arr = [12, 34, 54, 2, 3] 13 | n = len(arr) 14 | num = 3 15 | index = recSearch(arr, 0, n-1, num) 16 | if index != -1: 17 | print "Element %i is present at index %i" % (num, index) 18 | else: 19 | print "Element %i is not present" % (num) 20 | -------------------------------------------------------------------------------- /DuplicateInList/README.md: -------------------------------------------------------------------------------- 1 | # Duplicate in list 2 | **Problem Definition:** 3 | You are provided with a class Node that contains the properties: int data and Node next. Write a function named FindDuplicate 4 | that takes in the head of a linked list. The function should a linked list of duplicate numbers or `null` if all elements are unique. 5 | 6 | --- 7 | **Examples:*** 8 | * `[1] -> [2] -> [3] -> [4] ===> null` 9 | * `[1] -> [2] -> [3] -> [3] -> [4] ===> [3]` 10 | * `[1] -> [2] -> [1] -> [3] -> [2] ===> [1] -> [2]` 11 | 12 | --- 13 | **Notes:** 14 | * A commmon first approach uses nested for loop. However, to be most correct a hashing method should be used. 15 | -------------------------------------------------------------------------------- /IsPrime/Python/isPrime.py: -------------------------------------------------------------------------------- 1 | def isPrime(num): 2 | # Any number <= 1 is not prime 3 | if num <= 1: 4 | return False 5 | else: 6 | # We only need to loop through num/2 times because we can't have a factor 7 | # that is more than half the value of num. 8 | for i in range(2,num/2): 9 | # We only need to find one factor in order to make the number composite(not a prime number) 10 | if num % i == 0: 11 | return False 12 | # If nothing is found we can just return true 13 | return True 14 | 15 | # Test cases 16 | 17 | print(isPrime(2)) # => True 18 | print(isPrime(9)) # => False 19 | print(isPrime(41)) # => True 20 | -------------------------------------------------------------------------------- /CaesarCipher/README.md: -------------------------------------------------------------------------------- 1 | # Caesar Cipher 2 | **Problem Definition:** 3 | Define a function such that when given a string and a postitive integer shift value it will shift all the characters of the string by that amount 4 | 5 | --- 6 | **Examples:** 7 | * CaesarCipher("ATTACKATONCE", 4) # => 'EXXEGOEXSRGI' 8 | * CaesarCipher("NoChange", 0) # => 'NoChange' 9 | * CaesarCipher("JobInterviewAlgorithms", 4) # => 'NsfMrxivzmibEpksvmxlqw' 10 | --- 11 | **Notes:** 12 | * A Caesar Cipher is a type of substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. 13 | * A - Z corresponds to ASCII values 65-90 and a-z corresponds to ASCII values of 97-122 14 | -------------------------------------------------------------------------------- /AnagramCheck/Java/AnagramCheck.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class AnagramCheck { 4 | 5 | public static void main(String[] args){ 6 | //some test cases 7 | System.out.println(anagramCheck("Tom Marvolo Riddle", "I am Lord Voldemort")); 8 | System.out.println(anagramCheck("Hello", "olleH")); 9 | System.out.println(anagramCheck("Hi", "hello")); 10 | } 11 | 12 | public static boolean anagramCheck(String a, String b){ 13 | //make all letters lower case and remove spaces 14 | a = a.toLowerCase().replaceAll("\\s",""); 15 | b = b.toLowerCase().replaceAll("\\s",""); 16 | char aArr[] = a.toCharArray(); 17 | char bArr[] = b.toCharArray(); 18 | Arrays.sort(aArr); 19 | Arrays.sort(bArr); 20 | return Arrays.equals(aArr, bArr); 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Searching Algorithms/BinarySearch/README.md: -------------------------------------------------------------------------------- 1 | # Binary Search 2 | **Problem Definition:** 3 | This algorithm is tasked with finding a specified value in some sort of pre-sorted list(Array, Hash, Vector, etc.). 4 | It has a time complexity of O(log2(n)) - meaning it runs in logarithmic time. Simply, it finds the middle index of the given list and 5 | determines if the specified search value is greater or less than the middle index value you just found. Once it determines if it's 6 | greater or less than the given value it will cut the opposite half off from calculations and continue this process until either the 7 | specified search value is found or the list is empty. 8 | 9 | --- 10 | **Examples:** 11 | * `binarySearch(arrayBeingTested, specifiedSearchValue, sizeOfArray)` 12 | * `int test[5] = {6,4,7,8,3} => binarySearch(test, 4, 5) => | pos = 1 |` 13 | * `int test[5] = {6,4,7,8,3} => binarySearch(test, 8, 5) => | pos = 4 |` 14 | * `int test[5] = {6,4,7,8,3} => binarySearch(test, 1, 5) => | pos = -1 |` 15 | -------------------------------------------------------------------------------- /CaesarCipher/Python/CaesarCipher.py: -------------------------------------------------------------------------------- 1 | #ASCII CODES: 65-90 is A-Z ; 97-122 is a-z 2 | 3 | def CaesarCipher(str, shift): 4 | newStr = "" 5 | addNum = 0 6 | for i in str: 7 | print(newStr) 8 | # Is character Uppercase? 9 | if i.isupper(): 10 | # We take the current ASCII code and add some number from 0-25 11 | if (ord(i) + shift) > 90: 12 | addNum = (ord(i) + shift) - 90 13 | newStr += chr(65 + addNum) 14 | else: 15 | newStr += chr(ord(i) + shift) 16 | # If not, it's lowercase 17 | else: 18 | if(ord(i) + shift) > 122: 19 | addNum = (ord(i) + shift) - 122 20 | newStr += chr(97 + addNum) 21 | else: 22 | newStr += chr(ord(i) + shift) 23 | return newStr 24 | 25 | 26 | 27 | print(CaesarCipher("ATTACKATONCE", 4)) # => 'EXXEGOEXSRGI' 28 | print(CaesarCipher("NoChange", 0)) # => 'NoChange' 29 | print(CaesarCipher("JobInterviewAlgorithms", 4)) # => 'NsfMrxivzmibEpksvmxlqw' 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Austin Tice 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /DuplicateInList/C#/DuplicateInList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace DuplicateInList{ 5 | // Linked List Node Class 6 | class Node{ 7 | public int data; 8 | public Node next; // Points to the next node in the list 9 | 10 | public Node(int data){ 11 | this.data = data; 12 | } 13 | } 14 | 15 | class Program{ 16 | static void Main(string[] args){ 17 | Node head = InitLinkedList(); 18 | Console.WriteLine(string.Join (",", FindDuplicate(head))); 19 | } 20 | 21 | // Provided function 22 | static Node InitLinkedList(){ 23 | var head = new Node(0); 24 | var curr = head; 25 | 26 | for (int i = 1; i < 5; i++){ 27 | curr.next = new Node(i); 28 | curr = curr.next; 29 | } 30 | 31 | for (int i = 3; i < 10; i++){ 32 | curr.next = new Node(i); 33 | curr = curr.next; 34 | } 35 | return head; 36 | } 37 | 38 | // Function To Write 39 | static List FindDuplicate(Node node){ 40 | // HashSet is used for fastest lookup 41 | var foundNums = new HashSet(); 42 | var listDuplicates = new List(); 43 | 44 | // Loop until end of list 45 | while (node != null){ 46 | if (foundNums.Contains (node.data)) 47 | listDuplicates.Add (node.data); 48 | else 49 | foundNums.Add(node.data); 50 | 51 | node = node.next; 52 | } 53 | 54 | return listDuplicates; 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JobInterviewAlgorithms 2 | A library of classic algorithms that you will find **very** useful to study when interviewing for your next software engineering job. 3 | 4 | It consists of directories that contain classic job interview algorithms from all the top tech companies. Inside each folder there will be 5 | files that coincide with different programming languages' solutions. If you would like to write a new algorithm, or possibly add a new 6 | solution in a different language, feel free to submit a [pull request](https://github.com/AustinTice/JobInterviewAlgorithms/pulls). 7 | 8 | # Algorithms 9 | * [Anagram Check](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/AnagramCheck) 10 | * [Binary Search](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/Searching%20Algorithms/BinarySearch) 11 | * [Caesar Cipher](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/CaesarCipher) 12 | * [Duplicate In List](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/DuplicateInList) 13 | * [Factorial](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/Factorial) 14 | * [FizzBuzz](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/FizzBuzz) 15 | * [IsPrime](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/IsPrime) 16 | * [Recursive Linear Search](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/Searching%20Algorithms/RecursiveLinearSearch) 17 | * [Reverse Each Index](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/ReverseEachIndex) 18 | * [Reverse String](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/ReverseString) 19 | * [Square Root](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/SquareRoot) 20 | * [String Rotation](https://github.com/AustinTice/JobInterviewAlgorithms/tree/master/StringRotation) 21 | 22 | If you'd like to add a new feature or report a problem with any of the existing solutions feel free to do so [here](https://github.com/AustinTice/JobInterviewAlgorithms/issues) 23 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing New Algorithms Solutions 2 | Want to contribute new algorithms to this repository? 3 | 1. Create a directory in the `JobInterviewAlgorithms` directory. 4 | 2. Add a `README.md` file describing the algorithm and provide example of inputs and the desired outputs. This readme file should follow [this template](https://github.com/AustinTice/JobInterviewAlgorithms/blob/master/Templates/README-Template.md). 5 | 3. Create a directory for the programming language of your solution. 6 | 4. Add your solution! 7 | 8 | **NOTE:** _Before creating a Pull Request, please take a look at the Pull Request section below._ 9 | 10 | # Contributing Alternate Solutions 11 | It is possible to submit alternate solutions for an algorithm using the **same** language. Criteria for approval: 12 | 1. The alternate solution must use a different logic than the existing solutions (Example: a recursive solution and a solution using a loop). _**NOTE:** Small logic differences like the use of `while` loops instead of `for` loops will not be accepted as suitable differences in logic._ 13 | 2. Clear explanation must be provided in the Pull Request indicating why the proposed solution follows the previous criterion. 14 | 3. A `README.md` must be created (if none exist) within the same directory as the proposed solution indicating the reasoning behind the alternate solution. _**NOTE:** The explanations provided in the `README.md` file shall not _compare_ solutions to one another._ 15 | 16 | **NOTE:** _Before creating a Pull Request, please take a look at the Pull Request section below for details on naming conventions._ 17 | 18 | # Pull Requests (PR) 19 | Please assure that the guidelines below are followed when submitting a PR. 20 | ### Files and Directory structure 21 | The repository should retain the following file and directory structure: 22 | ``` 23 | JobInterviewAlgorithms 24 | -> AlgorithmName 25 | -> README.md 26 | -> ProgrammingLanguage 27 | -> AlgorithmName.ext 28 | ``` 29 | 30 | ### File and Directory Naming Convention 31 | CamelCase naming convention shall be used for **all** directories and files. Algorithms _may_ have more than one solution. If this is the case, the CamelCase naming convention shall be used along with a version number. The first solution shall not have a version number. 32 | 1. First solution: `AlgorithmName.ext` 33 | 2. Second solution: `AlgorithmName2.ext` 34 | 3. Any subsequent algorithm solution: `AlgorithmNameX.ext`, where X is the version number. 35 | 36 | # Issues 37 | Find an issue in one of the solutions? Open an [Issue](https://github.com/AustinTice/JobInterviewAlgorithms/issues/new)! 38 | 39 | To make things easier, templates will be available shortly for bug reports and solution requests! 40 | -------------------------------------------------------------------------------- /Searching Algorithms/BinarySearch/C++/BinarySearch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sortArray(int[], int); 4 | void swap(int*, int*); 5 | int binarySearch(int arr[], int, int); 6 | 7 | int main(){ 8 | // Initialize array of arbitrary data 9 | int test[5] = {6,4,7,8,3}; 10 | 11 | // We need to sort the array that was just processed - this algorithm uses bubble sort. 12 | sortArray(test, sizeof(test)/sizeof(test[0])); 13 | 14 | std::cout << "Binary Search - Position where value was found = " 15 | << binarySearch(test, 7, sizeof(test)/sizeof(test[0])) << std::endl; 16 | return 0; 17 | 18 | /* ----- TEST CASES ----- 19 | int test[5] = {6,4,7,8,3} => binarySearch(test, 4, 5) => | pos = 1 | 20 | int test[5] = {6,4,7,8,3} => binarySearch(test, 8, 5) => | pos = 4 | 21 | int test[5] = {6,4,7,8,3} => binarySearch(test, 1, 5) => | pos = -1 | 22 | */ 23 | } 24 | 25 | int binarySearch(int arr[], int value, int size){ 26 | // first element we choose to search with. 27 | int first = 0; 28 | // last element we search with. 29 | int last = size - 1; 30 | bool found = false; 31 | // Current position in the array - or return -1 if not found. 32 | int pos = -1; 33 | // Refers to the middle value of the array that we calculate below. 34 | int middle = 0; 35 | 36 | while(found != true && first <= last){ 37 | //Determines the middle value, and since it is an int it will truncate any decimal values. 38 | middle = (first+last)/2; 39 | if(arr[middle] == value){ 40 | found = true; 41 | pos = middle; 42 | } 43 | /* 44 | if the current iteration of middle is greater than the value we're searching for(since we 45 | assume that the array we're searching has already been sorted) we want the last value to 46 | equal the index just before where we just checked. This is because we know that the value 47 | cannot be after that. (REMEMBER: This only works if the array has already been sorted). 48 | */ 49 | else if(arr[middle] > value){ 50 | last = middle -1; 51 | } 52 | /* 53 | Refer to comment directly above the 'else-if' that represents the same logic, 54 | but for the first value instead of the last. 55 | */ 56 | else { 57 | first = middle +1; 58 | } 59 | } 60 | // The value we want to return is the index(pos) where the value we're looking for was found. 61 | return pos; 62 | } 63 | 64 | /* 65 | We're calling this function via the bubbleSort() - it allows us to 66 | swap the addresses of the current 2 elements being compared via pointers. 67 | */ 68 | void swap(int *x, int *y){ 69 | int temp = *x; 70 | *x = *y; 71 | *y = temp; 72 | } 73 | 74 | // This function implements the bubble sort algorithm technique. 75 | void sortArray(int arr[], int size){ 76 | for (int i = 0; i < size-1; i++) 77 | for (int j = 0; j < size-i-1; j++) 78 | if (arr[j] > arr[j+1]) 79 | swap(&arr[j], &arr[j+1]); 80 | } 81 | --------------------------------------------------------------------------------