├── .gitignore
├── .idea
├── .gitignore
├── misc.xml
└── modules.xml
├── IntroToAlgoCourse.iml
├── emptyFile.txt
└── src
├── Algos
├── BasicRecursion.java
├── BinarySearch.java
├── FizzBuzz.java
├── IntegerPalindrome.java
├── LinkedList.java
├── ListNode.java
├── MaximumDifference.java
├── MostRepeatedChar.java
├── MoveZeroes.java
├── Palindrome.java
├── PlusOne.java
├── PrimeNumber.java
├── SortArrayOfZeroOnes.java
├── Sortings.java
├── SumElementsInArray.java
└── TwoSum.java
├── Main.java
└── Tests
├── BasicRecursionTest.java
├── BinarySearchTest.java
├── FizzBuzzTest.java
├── IntegerPalindromeTest.java
├── LinkedListTest.java
├── MaximumDifferenceTest.java
├── MostRepeatedCharTest.java
├── MoveZeroesTest.java
├── PalindromeTest.java
├── PrimeNumberTest.java
├── SortArrayOfZeroOnesTest.java
├── SortingsTests.java
├── SumElementsInArrayTest.java
└── TwoSumTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
2 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
3 |
4 | .idea/
5 | # User-specific stuff
6 | .idea/**/workspace.xml
7 | .idea/**/tasks.xml
8 | .idea/**/usage.statistics.xml
9 | .idea/**/dictionaries
10 | .idea/**/shelf
11 |
12 | # Generated files
13 | .idea/**/contentModel.xml
14 |
15 | # Sensitive or high-churn files
16 | .idea/**/dataSources/
17 | .idea/**/dataSources.ids
18 | .idea/**/dataSources.local.xml
19 | .idea/**/sqlDataSources.xml
20 | .idea/**/dynamic.xml
21 | .idea/**/uiDesigner.xml
22 | .idea/**/dbnavigator.xml
23 |
24 | # Gradle
25 | .idea/**/gradle.xml
26 | .idea/**/libraries
27 |
28 | # Gradle and Maven with auto-import
29 | # When using Gradle or Maven with auto-import, you should exclude module files,
30 | # since they will be recreated, and may cause churn. Uncomment if using
31 | # auto-import.
32 | # .idea/artifacts
33 | # .idea/compiler.xml
34 | # .idea/jarRepositories.xml
35 | # .idea/modules.xml
36 | # .idea/*.iml
37 | # .idea/modules
38 | # *.iml
39 | # *.ipr
40 |
41 | # CMake
42 | cmake-build-*/
43 |
44 | # Mongo Explorer plugin
45 | .idea/**/mongoSettings.xml
46 |
47 | # File-based project format
48 | *.iws
49 |
50 | # IntelliJ
51 | out/
52 |
53 | # mpeltonen/sbt-idea plugin
54 | .idea_modules/
55 |
56 | # JIRA plugin
57 | atlassian-ide-plugin.xml
58 |
59 | # Cursive Clojure plugin
60 | .idea/replstate.xml
61 |
62 | # Crashlytics plugin (for Android Studio and IntelliJ)
63 | com_crashlytics_export_strings.xml
64 | crashlytics.properties
65 | crashlytics-build.properties
66 | fabric.properties
67 |
68 | # Editor-based Rest Client
69 | .idea/httpRequests
70 |
71 | # Android studio 3.1+ serialized cache file
72 | .idea/caches/build_file_checksums.ser
73 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Datasource local storage ignored files
5 | /dataSources/
6 | /dataSources.local.xml
7 | # Editor-based HTTP Client requests
8 | /httpRequests/
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/IntroToAlgoCourse.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/emptyFile.txt:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/engeniousio/coding_interview_class/14b95382e63de96e84f585ae70dc5b17a5dabc7a/emptyFile.txt
--------------------------------------------------------------------------------
/src/Algos/BasicRecursion.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class BasicRecursion {
4 |
5 | // 1, 1, 2, 3, 5, 8, 13, 21, 34 ...
6 |
7 | public int iterativeFibonacci(int number) {
8 |
9 | if (number == 1 || number == 2) {
10 | return 1;
11 | }
12 |
13 | int fibo1 = 1;
14 | int fibo2 = 1;
15 | int fibo = 1;
16 |
17 | for (int i = 3; i <= number; i++) {
18 | fibo = fibo1 + fibo2;
19 | fibo1 = fibo2;
20 | fibo2 = fibo;
21 | }
22 | return fibo;
23 |
24 | // TIME: O(n)
25 | // MEMORY: O(1)
26 | }
27 |
28 | // F(number) = F(number - 1) + F(number - 2)
29 | public int recursiveFibonacci(int number) {
30 |
31 | if (number == 1 || number == 2) {
32 | return 1;
33 | }
34 |
35 | return recursiveFibonacci(number - 1) + recursiveFibonacci(number - 2);
36 |
37 | // TIME: O(2^n)
38 | // Memory: O(n)
39 | }
40 |
41 | // 10! = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
42 | // F(n) = n * (n-1) * (n-2) * ... * 1
43 | // F(n-1) = (n-1) * (n-2) * ... * 1
44 | // F(n) = n * F(n-1)
45 |
46 | public int factorial(int n) {
47 |
48 | if (n == 1) {
49 | return 1;
50 | }
51 |
52 | return n * factorial(n - 1);
53 |
54 | // TIME: O(n)
55 | // MEMORY: O(n)
56 | }
57 |
58 |
59 | // 34324
60 | public int sumOfDigits(int n) {
61 |
62 | if (n < 10) {
63 | return n;
64 | }
65 |
66 | int reminder = n % 10 ;
67 | return reminder + sumOfDigits(n / 10);
68 |
69 | // TIME: O(lg(n))
70 | // MEMORY: O(lg(n))
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/Algos/BinarySearch.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | //1. Input: What is the input parameter?
4 | //2. Output: What is the datatype of your answer that your function has to return?
5 | //3. What data structures and methods associated with it will be used?
6 | //4. Where are you going to store temporary data (if needed)?
7 | //5. Describe the model of your algorithm before implementing solution in code
8 | //6. Write unit tests for your solution -- think about edge cases
9 | //7. Runtime Complexity?
10 | //8. Memory Complexity?
11 |
12 | public class BinarySearch {
13 |
14 |
15 | // given an array of integers and target; please find the location of target.
16 |
17 | // [10, -39, 100, 3, 6, 19, 2] ; target = 19
18 |
19 | public int searchAnElement(int[] arr, int target) {
20 | for(int i = 0; i < arr.length; i++) {
21 | if (arr[i] == target) {
22 | return i;
23 | }
24 | }
25 | return -1;
26 |
27 | // Time: O(n)
28 | // Memory: O(1)
29 | }
30 |
31 |
32 | // what if array is sorted?
33 |
34 | // [-39, 2, 3, 6, 10, 19, 100] ; target = 19
35 |
36 | // mid element = 6
37 | // 19 > 6 => target is in the right part
38 | // [10, 19, 100]
39 | // mid element = 19 == target => found!
40 |
41 | public int binarySearch(int[] arr, int target) {
42 | return binarySearchRecursion(arr, target, 0, arr.length - 1);
43 | }
44 |
45 | public int binarySearchRecursion(int arr[], int target, int l, int r) {
46 | int middleIndex = (l + r) / 2;
47 |
48 | if (r < l) {
49 | return -1;
50 | }
51 |
52 | if (arr[middleIndex] == target) {
53 | return middleIndex;
54 | } else if (target < arr[middleIndex]) {
55 | int newR = middleIndex - 1;
56 | return binarySearchRecursion(arr, target, l, newR);
57 | } else {
58 | int newL = middleIndex + 1;
59 | return binarySearchRecursion(arr, target, newL, r);
60 | }
61 |
62 | // TIME: O(log n)
63 | // MEMORY: O(1)
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/Algos/FizzBuzz.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | import java.util.ArrayList;
4 | import java.util.List;
5 |
6 | public class FizzBuzz {
7 |
8 | //Write a program that outputs the string representation of numbers from 1 to n.
9 | //But for multiples of three it should output “Fizz” instead of the number and for the
10 | // multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.
11 |
12 | public List fizzBuzz(int n) {
13 |
14 | List resultList = new ArrayList<>();
15 |
16 | for (int i = 1; i <= n; i++) {
17 | if (i % 15 == 0) {
18 | resultList.add("FizzBuzz");
19 | } else if (i % 3 == 0) {
20 | resultList.add("Fizz");
21 | } else if (i % 5 == 0) {
22 | resultList.add("Buzz");
23 | } else {
24 | resultList.add(String.valueOf(i));
25 | }
26 | }
27 | return resultList;
28 | }
29 | }
30 |
31 |
32 | // Time ~ O(n)
33 | // Memory - O(n)
--------------------------------------------------------------------------------
/src/Algos/IntegerPalindrome.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class IntegerPalindrome {
4 |
5 | // 1331
6 | // bite out "1" and save it
7 | // cut off "1"
8 | // bite off "3" and save "13"
9 | // cut off "3"
10 | // bite off "3" and save "133"
11 | // cut off "3"
12 | // bite off "1" and save "1331"
13 | // cut off "1"
14 | // compare "1331" and "1331"
15 |
16 | public boolean isPalindrome(int x) {
17 | if (x < 0) {
18 | return false;
19 | }
20 |
21 | int xOld = x;
22 | int reversedX = 0;
23 |
24 | while(x > 0) {
25 | int reminder = x % 10;
26 | reversedX = reversedX * 10 + reminder;
27 | x /= 10;
28 | }
29 | return xOld == reversedX;
30 | }
31 |
32 | // time: O(log10(n))
33 | // memory: O(1)
34 | }
35 |
36 |
37 | // 17 % 2 = 1
38 | // 17 % 10 = 7
39 | // 28563 % 10 = 3
40 | // 872364 % 10 = 4
41 |
42 | // 346872 / 10 = 34687
43 | // 8916273 / 10 = 891627
44 |
45 | // 133 reminder = 1 ==> 133 * 10 + 1 = 1330 + 1 = 1331
46 |
47 | // 5 / 10 = 0
48 |
49 |
50 | // log2(4) = 2
51 |
52 |
53 | // 100000 / 10 ? 6 times
--------------------------------------------------------------------------------
/src/Algos/LinkedList.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class LinkedList {
4 |
5 | public ListNode head = null;
6 | public ListNode tail = null;
7 |
8 | public void printLinkedList() {
9 | ListNode tmp = head;
10 | System.out.println();
11 | while(tmp != null) {
12 | System.out.print(tmp.val + " => ");
13 | tmp = tmp.next;
14 | }
15 | System.out.print("null");
16 | System.out.println();
17 |
18 | // TIME: O(n)
19 | }
20 |
21 | public void addNode(int val) {
22 | if (head == null) {
23 | ListNode tmp = new ListNode(val);
24 | head = tmp;
25 | tail = tmp;
26 | } else {
27 | tail.next = new ListNode(val);
28 | tail = tail.next;
29 | }
30 |
31 | // TIME: O(1)
32 | }
33 |
34 | public void addNodeAtStart(int val) {
35 | if (head == null) {
36 | ListNode tmp = new ListNode(val);
37 | head = tmp;
38 | tail = tmp;
39 | } else {
40 | ListNode tmp = new ListNode(val);
41 | tmp.next = head;
42 | head = tmp;
43 | }
44 |
45 | // TIME: O(1)
46 | }
47 |
48 | public void deleteNode(ListNode node) {
49 | node.val = node.next.val;
50 | node.next = node.next.next;
51 | }
52 |
53 | public boolean hasCycle(ListNode head) {
54 |
55 | if (head == null || head.next == null) {
56 | return false;
57 | }
58 |
59 | ListNode slow = head;
60 | ListNode fast = head.next;
61 |
62 | while (slow != fast) {
63 | if (fast == null || fast.next == null) {
64 | return false;
65 | }
66 | slow = slow.next;
67 | fast = fast.next.next;
68 | }
69 |
70 | return true;
71 |
72 | // TIME: O(n)
73 | // MEMORY: O(1)
74 | }
75 |
76 | public ListNode oddEvenList(ListNode head) {
77 | if (head == null) {
78 | return null;
79 | }
80 |
81 | ListNode odd = head;
82 | ListNode even = head.next;
83 | ListNode evenHead = even;
84 |
85 | while (even != null || even.next != null) {
86 | odd.next = even.next;
87 | odd = odd.next;
88 | even.next = odd.next;
89 | even = even.next;
90 | }
91 |
92 | odd.next = evenHead;
93 | return head;
94 |
95 | // TIME: O(n)
96 | // MEMORY: O(1)
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/src/Algos/ListNode.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class ListNode {
4 | int val;
5 | public ListNode next;
6 |
7 | public ListNode(int val) {
8 | this.val = val;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/Algos/MaximumDifference.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | import java.util.Arrays;
4 |
5 | public class MaximumDifference {
6 |
7 | // Given an array of integers, return the difference between
8 | // the largest and smallest integers in the array.
9 |
10 | // [2, 1, 9, -5, 10, 4] => 15
11 |
12 | // sorted array: [-5, 1, 2, 4, 9, 10]
13 |
14 | public int maximumDifferenceFirstDraft(int arr[]) {
15 | Arrays.sort(arr);
16 |
17 | if (arr.length < 1) {
18 | return -1;
19 | }
20 |
21 | int minElement = arr[0];
22 | int lastIndex = arr.length - 1;
23 | int maxElement = arr[lastIndex];
24 | return maxElement - minElement;
25 |
26 | // O(n * log(n))
27 | }
28 |
29 | public int maximumDifference(int arr[]) {
30 | if (arr.length < 1) {
31 | return -1;
32 | }
33 |
34 | int min = arr[0];
35 | int max = arr[0];
36 |
37 | for(int i = 0; i < arr.length; i++) {
38 | if (arr[i] > max) {
39 | max = arr[i];
40 | }
41 |
42 | if (arr[i] < min) {
43 | min = arr[i];
44 | }
45 | }
46 | return max - min;
47 | }
48 |
49 | // Time: O(n)
50 | // Memory: O(1)
51 | }
--------------------------------------------------------------------------------
/src/Algos/MostRepeatedChar.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | // 1. Input: What is the input parameter?
4 | // 2. Output: What is the datatype of your answer that your function has to return?
5 | // 3. What data structures and methods associated with it will be used?
6 | // 4. Where are you going to store temporary data (if needed)?
7 | // 5. Describe the model of your algorithm before implementing solution in code
8 | // 6. Write unit tests for your solution -- think about edge cases
9 | // 7. Runtime Complexity?
10 | // 8. Memory Complexity?
11 |
12 | import java.util.HashMap;
13 | import java.util.Iterator;
14 | import java.util.Map;
15 |
16 | // please find the most repeated character in string
17 | public class MostRepeatedChar {
18 |
19 | // init hashmap : < char : int > -- character and corresponding number of occurrences
20 | // iterate through every char
21 | // look up if you have the char in your hashmap storage
22 | // if you don't have this char: insert to a hashmap with value: 1
23 | // if you do have this char: increment value
24 |
25 | public char mostRepeatedChar(String str) {
26 | if (str.isEmpty()) {
27 | return '0';
28 | }
29 |
30 | HashMap result = new HashMap<>();
31 | for(int i =0; i< str.length(); i++) {
32 | Character character = str.charAt(i);
33 |
34 | if (!result.containsKey(character)) {
35 | result.put(character, 1);
36 | } else {
37 | int currentValue = result.get(character);
38 | currentValue++;
39 | result.put(character, currentValue);
40 | }
41 | }
42 |
43 | Iterator it = result.entrySet().iterator();
44 | int maxValue = 0;
45 | Character winner = str.charAt(0);
46 | while(it.hasNext()) {
47 | Map.Entry pair = (Map.Entry)it.next();
48 | if((Integer)pair.getValue() > maxValue) {
49 | maxValue = (Integer)pair.getValue();
50 | winner = (Character)pair.getKey();
51 | }
52 | }
53 | return winner;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Algos/MoveZeroes.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class MoveZeroes {
4 |
5 | // [0, 1, 0, 3, 12]
6 | // [1, 1, 0, 3, 12]
7 | // [1, 3, 0, 3, 12]
8 | // [1, 3, 12, 3, 12]
9 |
10 | // [1, 3, 12, 0, 0]
11 | public void moveZeroes(int[] nums) {
12 |
13 | int writePointer = 0;
14 |
15 | for (int readPointer = 0; readPointer < nums.length; readPointer++) {
16 | if (nums[readPointer] != 0) {
17 | nums[writePointer++] = nums[readPointer];
18 | }
19 | }
20 |
21 | for (int i = writePointer; i < nums.length; i++) {
22 | nums[i] = 0;
23 | }
24 |
25 | // Time: O(n)
26 | // Memory: O(1)
27 | }
28 | }
29 |
30 | // 15 => 3 * 5
31 | // 17 => prime
--------------------------------------------------------------------------------
/src/Algos/Palindrome.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | import javafx.beans.binding.StringBinding;
4 |
5 | public class Palindrome {
6 |
7 | // palindrome - a word, phrase, or sequence that reads the same backwards as forwards
8 | // abcba ; abddba; anna; racecar;
9 |
10 | // write a function that takes a string and returns boolean - whether this string is palindrome
11 |
12 | public boolean isPalindromeFirstDraft(String str) {
13 |
14 | String reversedStr = "";
15 |
16 | for (int i = str.length() -1; i>= 0; i--) {
17 | reversedStr += str.charAt(i);
18 | }
19 |
20 | return reversedStr.equals(str);
21 |
22 | // Time: O(n)
23 | // Memory: O(n)
24 | }
25 |
26 | public boolean isPalindrome(String str) {
27 | for (int i = 0; i < str.length() / 2; i++) {
28 | int leftChar = str.charAt(i);
29 | int rightChar = str.charAt(str.length() - 1 - i);
30 | if (leftChar != rightChar) {
31 | return false;
32 | }
33 | }
34 | return true;
35 |
36 | // Time: O(n)
37 | // Memory: O(1)
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Algos/PlusOne.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | public class PlusOne {
4 |
5 | // 1, 2, 9, 9, 9
6 | // 1, 2, 9, 10, 0
7 | // 1, 2, 10, 0, 0
8 | // 1, 3, 0, 0, 0
9 |
10 |
11 | // 9, 9, 9
12 | // 9, 9, 10
13 | // 9, 10, 0
14 | // 10, 0, 0
15 | // 1, 0, 0, 0
16 |
17 |
18 | public int[] plusOne(int[] digits) {
19 | digits[digits.length - 1] ++;
20 |
21 | for(int i = digits.length - 1; i>=1; i--) {
22 | if (digits[i] == 10) {
23 | digits[i] = 0;
24 | digits [i-1] += 1;
25 | }
26 | }
27 |
28 | if (digits[0] == 10) {
29 | digits[0] = 0;
30 | int [] array = new int[digits.length + 1];
31 | array[0] = 1;
32 | for(int i = 1; i 5 * 3
14 | // 17 => 1,17 ==> prime
15 |
16 | public boolean isPrime(int n) {
17 | if (n < 2) {
18 | return false;
19 | }
20 |
21 | for (int i = 2; i * i < n; i++) {
22 | if (n % i == 0) {
23 | return false;
24 | }
25 | }
26 | return true;
27 | }
28 |
29 | // Time: O(sqrt(n))
30 | // Memory: O(1)
31 | }
--------------------------------------------------------------------------------
/src/Algos/SortArrayOfZeroOnes.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | import java.lang.reflect.Array;
4 | import java.util.Arrays;
5 |
6 |
7 | // Given an array of integers.
8 | // All elements are either ones or zeros inside the array. Your goal is to order the array in ascending order.
9 |
10 | // [1, 0, 0, 0, 1, 1, 0]
11 |
12 | // [0, 0, 0, 0, 1, 1, 1]
13 |
14 | // 1. Input: What is the input parameter? => int arr[]
15 | // 2. Output: What is the datatype of your answer that your function has to return ? => void
16 | // 3. What data structures and methods associated with it will be used
17 | // 4. Where are you going to store temp data ( if needed )
18 | // 5. Describe model of your algorithm before implementing solution in code
19 | // 6. Write unit tests for your solution -- think about edge cases
20 | // 7. Runtime Complexity?
21 | // 8. Memory Complexity?
22 |
23 | public class SortArrayOfZeroOnes {
24 |
25 | public void sortArray(int arr[]) {
26 | int count = 0;
27 | for(int element: arr) {
28 | count += element;
29 | }
30 |
31 | int endOfZerosIndex = arr.length - count;
32 |
33 | for(int i = 0; i { -4, 0, 5, 6, 10, 100 }
23 |
24 | private void swap(int[] arr, int i, int j) {
25 | int tmp = arr[i];
26 | arr[i] = arr[j];
27 | arr[j] = tmp;
28 | }
29 |
30 |
31 | public void bubbleSort(int[] arr) {
32 | boolean swapDetected = true;
33 | while (swapDetected) {
34 | swapDetected = false;
35 | for (int i = 1; i < arr.length; i++) {
36 | if (arr[i] < arr[i - 1]) {
37 | swap(arr, i, i - 1);
38 | swapDetected = true;
39 | }
40 | }
41 | }
42 | // TIME: O(n^2)
43 | // MEMORY: O(1)
44 | }
45 |
46 | // {100, -4, 5, 0, 10, 6 }
47 |
48 | public void selectionSort(int[] arr) {
49 | for (int left = 0; left < arr.length; left++) {
50 | int minI = left;
51 | for(int i = left; i < arr.length; i++) {
52 | if (arr[i] < arr[minI]) {
53 | minI = i;
54 | }
55 | }
56 | swap(arr, left, minI);
57 | }
58 |
59 | // TIME: O(n^2)
60 | // MEMORY: O(1)
61 | }
62 |
63 | // {9, 5, 0, 10, 6, 5, 5}
64 |
65 | // 0 1 2 3 4 5 6 7 8 9 10
66 | // 1 3 1 1 1
67 |
68 | // 0, 5, 5, 5, 6, 9, 10
69 |
70 | public int[] countingSort(int[] arr, int maxValue) {
71 |
72 | int values[] = new int[maxValue + 1];
73 |
74 | for (int i = 0; i < arr.length; i++) {
75 | values[arr[i]]++;
76 | }
77 |
78 | int[] sortedArray = new int[arr.length];
79 | int k = 0;
80 |
81 | for(int i = 0; i < values.length; i++) {
82 | for(int j= 0; j < values[i]; j++) {
83 | sortedArray[k] = i;
84 | k++;
85 | }
86 | }
87 | return sortedArray;
88 |
89 | // TIME: O(n+k) -- where k is max element
90 | // MEMORY: O(k), k is max element
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/Algos/SumElementsInArray.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 | // 1. Input: What is the input parameter?
4 | // 2. Output: What is the datatype of your answer that your function has to return?
5 | // 3. What data structures and methods associated with it will be used
6 | // 4. Where are you going to store temp data ( if needed )
7 | // 5. Describe the model of your algorithm before implementing solution in code
8 | // 6. Write unit tests for your solution -- think about edge cases
9 | // 7. Runtime Complexity?
10 | // 8. Memory Complexity?
11 |
12 |
13 | public class SumElementsInArray {
14 | // You are given an array of integers. Please return sum of all elements
15 |
16 | // [5, 1, 10, 24, -4, 10, 5, 3, 6, 7, 0, 20, 30] ----> n elements
17 | public int sumElements(int arr[]) {
18 | int sum = 0;
19 | for(int element: arr) {
20 | sum += element;
21 | }
22 | return sum;
23 | }
24 |
25 | // O(1)
26 | public int firstElement(int arr[]) {
27 | if (arr.length == 0) {
28 | return -1;
29 | }
30 |
31 | return arr[0];
32 | }
33 | }
34 |
35 | // How much time does the execution take?
36 | // 1. Hardware?
37 | // 2. Other processes in background
38 | // 3. Programming languages
39 | // Number of operation
40 |
41 |
42 | // Runtime complexity: n + 2 => ~n => O(n) -> linear time
43 |
44 | // Memory complexity: O(1) -- constant
--------------------------------------------------------------------------------
/src/Algos/TwoSum.java:
--------------------------------------------------------------------------------
1 | package Algos;
2 |
3 |
4 | // 1. Input: What is the input parameter?
5 | // 2. Output: What is the datatype of your answer that your function has to return?
6 | // 3. What data structures and methods associated with it will be used?
7 | // 4. Where are you going to store temporary data (if needed)?
8 | // 5. Describe the model of your algorithm before implementing solution in code
9 | // 6. Write unit tests for your solution -- think about edge cases
10 | // 7. Runtime Complexity?
11 | // 8. Memory Complexity?
12 |
13 | import java.util.HashMap;
14 | import java.util.Map;
15 |
16 | public class TwoSum {
17 |
18 | // Given nums = [2, 7, 11, 15], target = 9,
19 |
20 | public int[] twoSumBruteForce(int[] nums, int target) {
21 | for (int i = 0; i < nums.length; i++) {
22 | for (int j = 0; j < nums.length; j++) {
23 | int num1 = nums[i];
24 | int num2 = nums[j];
25 | if (i != j && num1 + num2 == target) {
26 | return new int[]{i, j};
27 | }
28 | }
29 | }
30 | return new int[]{0, 0};
31 | // Time: O(n^2)
32 | // Memory: O(1)
33 | }
34 |
35 | public int[] twoSum(int[] nums, int target) {
36 | Map map = new HashMap<>();
37 | for(int i= 0; i < nums.length; i++) {
38 | map.put(nums[i], i);
39 | }
40 |
41 | for(int i=0; i < nums.length; i++) {
42 | int num1 = nums[i];
43 | int complement = target - num1;
44 | if (map.containsKey(complement) && i != map.get(complement)) {
45 | int indexOfnum2 = map.get(complement);
46 | return new int[] { i, indexOfnum2 };
47 | }
48 | }
49 | return new int[]{0, 0};
50 |
51 | // Time: O(n)
52 | // Memory: O(n)
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Main.java:
--------------------------------------------------------------------------------
1 | import Algos.SortArrayOfZeroOnes;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 |
6 | public class Main {
7 |
8 | public static void main(String[] args) {
9 | sortArrayOfZeroOnes();
10 | }
11 |
12 | static void sortArrayOfZeroOnes() {
13 | // int arr[] = {0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0};
14 | int arr[] = {1, 1, 1, 1, 1, 1, 1, 0};
15 | SortArrayOfZeroOnes sortArrayOfZeroOnes = new SortArrayOfZeroOnes();
16 | sortArrayOfZeroOnes.sortArray(arr);
17 | System.out.println(Arrays.toString(arr));
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/src/Tests/BasicRecursionTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.BasicRecursion;
4 | import Algos.IntegerPalindrome;
5 | import org.junit.Assert;
6 |
7 | public class BasicRecursionTest {
8 |
9 | @org.junit.jupiter.api.Test
10 | void iterativeFiboTest() {
11 | int number = 9;
12 | BasicRecursion basicRecursion = new BasicRecursion();
13 | int expectedResult = 34;
14 | int actualResult = basicRecursion.iterativeFibonacci(number);
15 | Assert.assertEquals(expectedResult, actualResult);
16 | }
17 |
18 | @org.junit.jupiter.api.Test
19 | void recursionFiboTest() {
20 | int number = 9;
21 | BasicRecursion basicRecursion = new BasicRecursion();
22 | int expectedResult = 34;
23 | int actualResult = basicRecursion.recursiveFibonacci(number);
24 | Assert.assertEquals(expectedResult, actualResult);
25 | }
26 |
27 | @org.junit.jupiter.api.Test
28 | void recursionFactorialTest() {
29 | int number = 5;
30 | BasicRecursion basicRecursion = new BasicRecursion();
31 | int expectedResult = 120;
32 | int actualResult = basicRecursion.factorial(number);
33 | Assert.assertEquals(expectedResult, actualResult);
34 | }
35 |
36 | @org.junit.jupiter.api.Test
37 | void recursionSumOfDigitsTest() {
38 | int number = 12345;
39 | BasicRecursion basicRecursion = new BasicRecursion();
40 | int expectedResult = 15;
41 | int actualResult = basicRecursion.sumOfDigits(number);
42 | Assert.assertEquals(expectedResult, actualResult);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/Tests/BinarySearchTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.BinarySearch;
4 | import org.junit.Assert;
5 |
6 | public class BinarySearchTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void searchAnElementTest() {
10 | int arr[] = {10, -100, 7, 19, 9, 10, 5};
11 | int target = -100;
12 | int expectedResult = 1;
13 | BinarySearch binarySearch = new BinarySearch();
14 | int actualResult = binarySearch.searchAnElement(arr, target);
15 | Assert.assertEquals(expectedResult, actualResult);
16 | }
17 |
18 | @org.junit.jupiter.api.Test
19 | void binarySearchTest() {
20 | int arr[] = {-100, 5, 7, 9, 10, 19, 123, 10000};
21 | int target = 123;
22 | int expectedResult = 6;
23 | BinarySearch binarySearch = new BinarySearch();
24 | int actualResult = binarySearch.binarySearch(arr, target);
25 | Assert.assertEquals(expectedResult, actualResult);
26 | }
27 |
28 | @org.junit.jupiter.api.Test
29 | void binarySearchTest2() {
30 | int arr[] = {-100, 5, 7, 9, 10, 19, 123, 10000};
31 | int target = -19;
32 | int expectedResult = -1;
33 | BinarySearch binarySearch = new BinarySearch();
34 | int actualResult = binarySearch.binarySearch(arr, target);
35 | Assert.assertEquals(expectedResult, actualResult);
36 | }
37 |
38 | @org.junit.jupiter.api.Test
39 | void binarySearchTest3() {
40 | int arr[] = {-100, 5, 7, 9, 10, 19, 123, 10000};
41 | int target = -100;
42 | int expectedResult = 0;
43 | BinarySearch binarySearch = new BinarySearch();
44 | int actualResult = binarySearch.binarySearch(arr, target);
45 | Assert.assertEquals(expectedResult, actualResult);
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/Tests/FizzBuzzTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.FizzBuzz;
4 | import org.junit.Assert;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 |
9 | public class FizzBuzzTest {
10 |
11 | @org.junit.jupiter.api.Test
12 | void fizzBuzz5() {
13 |
14 | int n = 5;
15 |
16 | List expectedResult = new ArrayList<>();
17 | expectedResult.add("1");
18 | expectedResult.add("2");
19 | expectedResult.add("Fizz");
20 | expectedResult.add("4");
21 | expectedResult.add("Buzz");
22 |
23 | FizzBuzz fizzBuzz = new FizzBuzz();
24 | List actualResult = fizzBuzz.fizzBuzz(n);
25 | Assert.assertEquals(expectedResult, actualResult);
26 | }
27 |
28 | @org.junit.jupiter.api.Test
29 | void fizzBuzz15() {
30 |
31 | int n = 15;
32 |
33 | List expectedResult = new ArrayList<>();
34 | expectedResult.add("1");
35 | expectedResult.add("2");
36 | expectedResult.add("Fizz");
37 | expectedResult.add("4");
38 | expectedResult.add("Buzz");
39 | expectedResult.add("Fizz");
40 | expectedResult.add("7");
41 | expectedResult.add("8");
42 | expectedResult.add("Fizz");
43 | expectedResult.add("Buzz");
44 | expectedResult.add("11");
45 | expectedResult.add("Fizz");
46 | expectedResult.add("13");
47 | expectedResult.add("14");
48 | expectedResult.add("FizzBuzz");
49 |
50 | FizzBuzz fizzBuzz = new FizzBuzz();
51 | List actualResult = fizzBuzz.fizzBuzz(n);
52 | Assert.assertEquals(expectedResult, actualResult);
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Tests/IntegerPalindromeTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.IntegerPalindrome;
4 | import org.junit.Assert;
5 |
6 | public class IntegerPalindromeTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void integerPalindromeTest() {
10 | int x = 1234321;
11 | IntegerPalindrome integerPalindrome = new IntegerPalindrome();
12 | boolean actualResult = integerPalindrome.isPalindrome(x);
13 | Assert.assertTrue(actualResult);
14 | }
15 |
16 | @org.junit.jupiter.api.Test
17 | void integerNotPalindromeTest() {
18 | int x = 1934321;
19 | IntegerPalindrome integerPalindrome = new IntegerPalindrome();
20 | boolean actualResult = integerPalindrome.isPalindrome(x);
21 | Assert.assertFalse(actualResult);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/Tests/LinkedListTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.LinkedList;
4 | import Algos.ListNode;
5 | import org.junit.Assert;
6 |
7 | public class LinkedListTest {
8 |
9 | @org.junit.jupiter.api.Test
10 | void printLLTest() {
11 | ListNode first = new ListNode(1);
12 | ListNode second = new ListNode(2);
13 | ListNode third = new ListNode(3);
14 | ListNode fourth = new ListNode(4);
15 | ListNode fifth = new ListNode(5);
16 |
17 | LinkedList linkedList = new LinkedList();
18 | linkedList.head = first;
19 | first.next = second;
20 | second.next = third;
21 | third.next = fourth;
22 | fourth.next = fifth;
23 |
24 | linkedList.printLinkedList();
25 | }
26 |
27 | @org.junit.jupiter.api.Test
28 | void addNodeTest() {
29 | LinkedList linkedList = new LinkedList();
30 | linkedList.addNode(1);
31 | linkedList.addNode(2);
32 | linkedList.addNode(3);
33 | linkedList.addNode(4);
34 | linkedList.addNode(5);
35 |
36 | linkedList.printLinkedList();
37 | }
38 |
39 | @org.junit.jupiter.api.Test
40 | void addNodeAtStartTest() {
41 | LinkedList linkedList = new LinkedList();
42 | linkedList.addNode(2);
43 | linkedList.addNode(3);
44 | linkedList.addNode(4);
45 | linkedList.addNode(5);
46 |
47 | linkedList.addNodeAtStart(1);
48 |
49 | linkedList.printLinkedList();
50 | }
51 |
52 | @org.junit.jupiter.api.Test
53 | void deleteNodeTest() {
54 | ListNode first = new ListNode(1);
55 | ListNode second = new ListNode(2);
56 | ListNode third = new ListNode(3);
57 | ListNode fourth = new ListNode(4);
58 | ListNode fifth = new ListNode(5);
59 |
60 | LinkedList linkedList = new LinkedList();
61 | linkedList.head = first;
62 | first.next = second;
63 | second.next = third;
64 | third.next = fourth;
65 | fourth.next = fifth;
66 |
67 | linkedList.deleteNode(fourth);
68 |
69 | linkedList.printLinkedList();
70 | }
71 |
72 |
73 | @org.junit.jupiter.api.Test
74 | void cycleTest() {
75 | ListNode first = new ListNode(1);
76 | ListNode second = new ListNode(2);
77 | ListNode third = new ListNode(3);
78 | ListNode fourth = new ListNode(4);
79 | ListNode fifth = new ListNode(5);
80 |
81 | LinkedList linkedList = new LinkedList();
82 | linkedList.head = first;
83 | first.next = second;
84 | second.next = third;
85 | third.next = fourth;
86 | fourth.next = second;
87 |
88 | boolean hasCycle = linkedList.hasCycle(linkedList.head);
89 | Assert.assertTrue(hasCycle);
90 | }
91 |
92 | @org.junit.jupiter.api.Test
93 | void noCycleTest() {
94 | ListNode first = new ListNode(1);
95 | ListNode second = new ListNode(2);
96 | ListNode third = new ListNode(3);
97 | ListNode fourth = new ListNode(4);
98 | ListNode fifth = new ListNode(5);
99 |
100 | LinkedList linkedList = new LinkedList();
101 | linkedList.head = first;
102 | first.next = second;
103 | second.next = third;
104 | third.next = fourth;
105 | fourth.next = fifth;
106 |
107 | boolean hasCycle = linkedList.hasCycle(linkedList.head);
108 | Assert.assertFalse(hasCycle);
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/Tests/MaximumDifferenceTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.MaximumDifference;
4 | import org.junit.Assert;
5 |
6 | public class MaximumDifferenceTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void maxDifferenceTest() {
10 |
11 | int arr[] = {2, 1, 9, -5, 10, 4};
12 | int expectedResult = 15;
13 |
14 | MaximumDifference maximumDifference = new MaximumDifference();
15 | int actualResult = maximumDifference.maximumDifference(arr);
16 | Assert.assertEquals(expectedResult, actualResult);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/src/Tests/MostRepeatedCharTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.IntegerPalindrome;
4 | import Algos.MostRepeatedChar;
5 | import org.junit.Assert;
6 |
7 | public class MostRepeatedCharTest {
8 |
9 | @org.junit.jupiter.api.Test
10 | void mostRepeatedCharTest() {
11 | String str = "Hello, World!";
12 | Character expectedResult = 'l';
13 |
14 | MostRepeatedChar mostRepeatedChar = new MostRepeatedChar();
15 | Character actualResult = mostRepeatedChar.mostRepeatedChar(str);
16 | Assert.assertEquals(expectedResult, actualResult);
17 | }
18 |
19 | }
20 |
--------------------------------------------------------------------------------
/src/Tests/MoveZeroesTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.MoveZeroes;
4 | import Algos.TwoSum;
5 | import org.junit.Assert;
6 |
7 | public class MoveZeroesTest {
8 |
9 | @org.junit.jupiter.api.Test
10 | void testMoveZeroes() {
11 | MoveZeroes moveZeroes = new MoveZeroes();
12 | int array[] = {0,1,0,3,12};
13 | int[] expectedResult = new int[]{1, 3, 12, 0, 0};
14 | moveZeroes.moveZeroes(array);
15 | Assert.assertArrayEquals(array, expectedResult);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/Tests/PalindromeTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.Palindrome;
4 | import org.junit.Assert;
5 |
6 | public class PalindromeTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void testPalindrome() {
10 | String str = "racecar";
11 | Palindrome palindrome = new Palindrome();
12 |
13 | boolean expectedResult = true;
14 | boolean actualResult = palindrome.isPalindrome(str);
15 |
16 | Assert.assertEquals(expectedResult, actualResult);
17 | }
18 |
19 | @org.junit.jupiter.api.Test
20 | void testFalsePalindrome() {
21 | String str = "abcdbcaa";
22 | Palindrome palindrome = new Palindrome();
23 |
24 | boolean expectedResult = false;
25 | boolean actualResult = palindrome.isPalindrome(str);
26 |
27 | Assert.assertEquals(expectedResult, actualResult);
28 | }
29 |
30 |
31 | @org.junit.jupiter.api.Test
32 | void testEmptyPalindrome() {
33 | String str = "";
34 | Palindrome palindrome = new Palindrome();
35 |
36 | boolean expectedResult = true;
37 | boolean actualResult = palindrome.isPalindrome(str);
38 |
39 | Assert.assertEquals(expectedResult, actualResult);
40 | }
41 |
42 | @org.junit.jupiter.api.Test
43 | void testOneLetterPalindrome() {
44 | String str = "s";
45 | Palindrome palindrome = new Palindrome();
46 |
47 | boolean expectedResult = true;
48 | boolean actualResult = palindrome.isPalindrome(str);
49 |
50 | Assert.assertEquals(expectedResult, actualResult);
51 | }
52 |
53 | @org.junit.jupiter.api.Test
54 | void testEvenPalindrome() {
55 | String str = "abba";
56 | Palindrome palindrome = new Palindrome();
57 |
58 | boolean expectedResult = true;
59 | boolean actualResult = palindrome.isPalindrome(str);
60 |
61 | Assert.assertEquals(expectedResult, actualResult);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Tests/PrimeNumberTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.PrimeNumber;
4 | import org.junit.Assert;
5 |
6 | public class PrimeNumberTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void testPrime() {
10 | PrimeNumber primeNumber = new PrimeNumber();
11 | int n = 17;
12 | Assert.assertTrue(primeNumber.isPrime(n));
13 | }
14 |
15 | @org.junit.jupiter.api.Test
16 | void testPrimeOne() {
17 | PrimeNumber primeNumber = new PrimeNumber();
18 | int n = 1;
19 | Assert.assertFalse(primeNumber.isPrime(n));
20 | }
21 |
22 | @org.junit.jupiter.api.Test
23 | void testPrimeFalse() {
24 | PrimeNumber primeNumber = new PrimeNumber();
25 | int n = 36;
26 | Assert.assertFalse(primeNumber.isPrime(n));
27 | }
28 |
29 | @org.junit.jupiter.api.Test
30 | void testPrimeTwo() {
31 | PrimeNumber primeNumber = new PrimeNumber();
32 | int n = 2;
33 | Assert.assertTrue(primeNumber.isPrime(n));
34 | }
35 |
36 | @org.junit.jupiter.api.Test
37 | void testPrimeThree() {
38 | PrimeNumber primeNumber = new PrimeNumber();
39 | int n = 3;
40 | Assert.assertTrue(primeNumber.isPrime(n));
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Tests/SortArrayOfZeroOnesTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.SortArrayOfZeroOnes;
4 | import org.junit.Assert;
5 |
6 | class SortArrayOfZeroOnesTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void sortArray() {
10 | SortArrayOfZeroOnes sortArrayOfZeroOnes = new SortArrayOfZeroOnes();
11 |
12 | int arr[] = {0, 1, 1, 0, 0, 1, 1, 1, 0};
13 | sortArrayOfZeroOnes.sortArray(arr);
14 | int expectedValue[] = {0, 0, 0, 0, 1, 1, 1, 1, 1};
15 | Assert.assertArrayEquals(arr, expectedValue);
16 | }
17 |
18 | @org.junit.jupiter.api.Test
19 | void sortEmptyArray() {
20 | SortArrayOfZeroOnes sortArrayOfZeroOnes = new SortArrayOfZeroOnes();
21 |
22 | int arr[] = {};
23 | sortArrayOfZeroOnes.sortArray(arr);
24 | int expectedValue[] = {};
25 | Assert.assertArrayEquals(arr, expectedValue);
26 | }
27 |
28 | @org.junit.jupiter.api.Test
29 | void sortOneElementArray() {
30 | SortArrayOfZeroOnes sortArrayOfZeroOnes = new SortArrayOfZeroOnes();
31 |
32 | int arr[] = {1};
33 | sortArrayOfZeroOnes.sortArray(arr);
34 | int expectedValue[] = {1};
35 | Assert.assertArrayEquals(arr, expectedValue);
36 | }
37 | }
--------------------------------------------------------------------------------
/src/Tests/SortingsTests.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.Sortings;
4 | import org.junit.Assert;
5 |
6 | import java.util.Arrays;
7 |
8 | public class SortingsTests {
9 |
10 |
11 | @org.junit.jupiter.api.Test
12 | void bubbleSort() {
13 | int[] arr = {100, -4, 5, 0, 10, 6};
14 | int[] expectedResult = {-4, 0, 5, 6, 10, 100};
15 | Sortings sortings = new Sortings();
16 | sortings.bubbleSort(arr);
17 | Assert.assertArrayEquals(expectedResult, arr);
18 | }
19 |
20 | @org.junit.jupiter.api.Test
21 | void selectionSort() {
22 | int[] arr = {100, -4, 5, 0, 10, 6};
23 | int[] expectedResult = {-4, 0, 5, 6, 10, 100};
24 | Sortings sortings = new Sortings();
25 | sortings.selectionSort(arr);
26 | Assert.assertArrayEquals(expectedResult, arr);
27 | }
28 |
29 | @org.junit.jupiter.api.Test
30 | void countingSort() {
31 | int[] arr = {9, 5, 0, 10, 6, 5, 5};
32 | int[] expectedResult = {0, 5, 5, 5, 6, 9, 10};
33 | Sortings sortings = new Sortings();
34 | int[] actualResult = sortings.countingSort(arr, 10);
35 | Assert.assertArrayEquals(expectedResult, actualResult);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/Tests/SumElementsInArrayTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.SumElementsInArray;
4 | import org.junit.Assert;
5 |
6 | public class SumElementsInArrayTest {
7 |
8 | @org.junit.jupiter.api.Test
9 | void testSumElements() {
10 | SumElementsInArray sumElementsInArray = new SumElementsInArray();
11 | int array[] = {1, 5, -5, 10, 4};
12 | int expectedResult = 15;
13 | int actualResult = sumElementsInArray.sumElements(array);
14 | Assert.assertEquals(expectedResult, actualResult);
15 | }
16 |
17 | @org.junit.jupiter.api.Test
18 | void testSumEmptyElements() {
19 | SumElementsInArray sumElementsInArray = new SumElementsInArray();
20 | int array[] = {};
21 | int expectedResult = 0;
22 | int actualResult = sumElementsInArray.sumElements(array);
23 | Assert.assertEquals(expectedResult, actualResult);
24 | }
25 |
26 | @org.junit.jupiter.api.Test
27 | void testSumOneElement() {
28 | SumElementsInArray sumElementsInArray = new SumElementsInArray();
29 | int array[] = {5};
30 | int expectedResult = 5;
31 | int actualResult = sumElementsInArray.sumElements(array);
32 | Assert.assertEquals(expectedResult, actualResult);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Tests/TwoSumTest.java:
--------------------------------------------------------------------------------
1 | package Tests;
2 |
3 | import Algos.SumElementsInArray;
4 | import Algos.TwoSum;
5 | import org.junit.Assert;
6 |
7 | public class TwoSumTest {
8 |
9 | @org.junit.jupiter.api.Test
10 | void testTwoSumBruteForce() {
11 | TwoSum twoSum = new TwoSum();
12 | int array[] = {2, 7, 11, 15};
13 | int target = 9;
14 | int[] expectedResult = new int[]{0,1};
15 | int[] actualResult = twoSum.twoSumBruteForce(array, target);
16 | Assert.assertArrayEquals(expectedResult, actualResult);
17 | }
18 |
19 | @org.junit.jupiter.api.Test
20 | void testTwoSumBruteForceSame() {
21 | TwoSum twoSum = new TwoSum();
22 | int array[] = {3, 2, 4};
23 | int target = 6;
24 | int[] expectedResult = new int[]{1,2};
25 | int[] actualResult = twoSum.twoSumBruteForce(array, target);
26 | Assert.assertArrayEquals(expectedResult, actualResult);
27 | }
28 |
29 | @org.junit.jupiter.api.Test
30 | void testTwoSum() {
31 | TwoSum twoSum = new TwoSum();
32 | int array[] = {2, 7, 11, 15};
33 | int target = 9;
34 | int[] expectedResult = new int[]{0,1};
35 | int[] actualResult = twoSum.twoSum(array, target);
36 | Assert.assertArrayEquals(expectedResult, actualResult);
37 | }
38 |
39 | @org.junit.jupiter.api.Test
40 | void testTwoSumSame() {
41 | TwoSum twoSum = new TwoSum();
42 | int array[] = {3, 2, 4};
43 | int target = 6;
44 | int[] expectedResult = new int[]{1,2};
45 | int[] actualResult = twoSum.twoSum(array, target);
46 | Assert.assertArrayEquals(expectedResult, actualResult);
47 | }
48 | }
49 |
--------------------------------------------------------------------------------