├── .github └── FUNDING.yml ├── src ├── inheritance │ ├── Animal.java │ ├── README.md │ └── Cat.java ├── SortArray.java ├── PrintFibonacciSeries.java ├── StringContainsVowels.java ├── ReverseString.java ├── PrimeNumberCheck.java ├── IntegerArraySum.java ├── ReverseALinkedList.java ├── CheckPalindromeString.java ├── RemoveAChar.java ├── ShuffleArray.java ├── RemoveWhiteSpaces.java ├── BinarySearch.java └── MergeSort.java ├── LICENSE └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jdevstatic 2 | -------------------------------------------------------------------------------- /src/inheritance/Animal.java: -------------------------------------------------------------------------------- 1 | class Animal { 2 | String family = "cats"; 3 | } -------------------------------------------------------------------------------- /src/SortArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class SortArray { 4 | 5 | public static void main(String[] args) { 6 | 7 | int[] array = {1, 2, 3, -1, -2, 4}; 8 | 9 | Arrays.sort(array); 10 | 11 | System.out.println(Arrays.toString(array)); 12 | 13 | } 14 | 15 | } -------------------------------------------------------------------------------- /src/inheritance/README.md: -------------------------------------------------------------------------------- 1 | ## Inheritance 2 | In order to implement inheritance, 3 | it needs to be separated in one folder, 4 | 5 | so if you are in `src` folder, 6 | 7 | ``` 8 | cd inheritance 9 | ``` 10 | 11 | then, we need to compile it first 12 | since we will be using another class 13 | 14 | ``` 15 | javac Animal.java Cat.java 16 | ``` 17 | 18 | then 19 | 20 | ``` 21 | java Cat 22 | ``` 23 | -------------------------------------------------------------------------------- /src/PrintFibonacciSeries.java: -------------------------------------------------------------------------------- 1 | public class PrintFibonacciSeries { 2 | 3 | public static void printFibonacciSeries(int count) { 4 | int a = 0; 5 | int b = 1; 6 | int c = 1; 7 | for (int i = 1; i <= count; i++) { 8 | System.out.print(a + ", "); 9 | a = b; 10 | b = c; 11 | c = a + b; 12 | } 13 | System.out.println(""); 14 | } 15 | 16 | public static void main(String args[]) { 17 | 18 | // don't exceed it hehe 19 | printFibonacciSeries(40); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/StringContainsVowels.java: -------------------------------------------------------------------------------- 1 | public class StringContainsVowels { 2 | 3 | public static boolean 4 | stringContainsVowels(String input) { 5 | 6 | return input.toLowerCase().matches(".*[aeiou].*"); 7 | 8 | } 9 | 10 | public static void main(String[] args) { 11 | 12 | String input = "fdfd"; 13 | 14 | System.out.print(input + " "); 15 | 16 | if (stringContainsVowels(input) == true) { 17 | System.out.println("contains vowel"); 18 | } else { 19 | System.out.println("does not contain vowel"); 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/ReverseString.java: -------------------------------------------------------------------------------- 1 | public class ReverseString { 2 | 3 | public static void main(String[] args) { 4 | 5 | String str = "789"; 6 | System.out.println(reverseString(str)); 7 | } 8 | 9 | public static String reverseString(String in) { 10 | if (in == null) 11 | throw new IllegalArgumentException("input null"); 12 | 13 | StringBuilder out = new StringBuilder(); 14 | 15 | char[] chars = in.toCharArray(); 16 | 17 | for (int i = chars.length - 1; i >= 0; i--) 18 | out.append(chars[i]); 19 | 20 | return out.toString(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/inheritance/Cat.java: -------------------------------------------------------------------------------- 1 | 2 | class Cat extends Animal { 3 | void meow(){ 4 | System.out.print("meow"); 5 | } 6 | 7 | public static void main(String[] args) { 8 | 9 | Cat mingming = new Cat(); 10 | 11 | //family is coming from Animal class 12 | System.out.print("call from Animal class, family is : `"); 13 | System.out.print(mingming.family); 14 | System.out.println("`"); 15 | 16 | //meuw method is from Cat class 17 | System.out.print("call from Cat class, method will `"); 18 | mingming.meow(); 19 | System.out.println("`"); 20 | 21 | } 22 | 23 | } 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/PrimeNumberCheck.java: -------------------------------------------------------------------------------- 1 | public class PrimeNumberCheck { 2 | 3 | public static void main(String[] args) { 4 | 5 | int n; 6 | 7 | n = 12; 8 | 9 | System.out.print("is "); 10 | System.out.print(n); 11 | System.out.print(" prime number ? "); 12 | System.out.println(isPrime(n)); 13 | } 14 | 15 | public static boolean isPrime(int n) { 16 | if (n == 0 || n == 1) { 17 | return false; 18 | } 19 | if (n == 2) { 20 | return true; 21 | } 22 | for (int i = 2; i <= n / 2; i++) { 23 | if (n % i == 0) { 24 | return false; 25 | } 26 | } 27 | 28 | return true; 29 | } 30 | } -------------------------------------------------------------------------------- /src/IntegerArraySum.java: -------------------------------------------------------------------------------- 1 | public class IntegerArraySum { 2 | 3 | public static void main(String[] args) { 4 | 5 | int[] array = { 10, 20, 30 }; 6 | 7 | int sum = 0; 8 | 9 | for (int i : array) 10 | sum += i; 11 | 12 | System.out.print("sum of the given array { "); 13 | System.out.print(array[0]); 14 | System.out.print(", "); 15 | System.out.print(array[1]); 16 | System.out.print(", "); 17 | System.out.print(array[2]); 18 | System.out.print(", "); 19 | System.out.print("} is "); 20 | System.out.println(sum); 21 | 22 | } 23 | } -------------------------------------------------------------------------------- /src/ReverseALinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | 3 | public class ReverseALinkedList { 4 | 5 | public static void main(String[] args) { 6 | 7 | LinkedList linkedlist = new LinkedList<>(); 8 | 9 | linkedlist.add(1); 10 | linkedlist.add(2); 11 | linkedlist.add(3); 12 | 13 | System.out.print(linkedlist); 14 | 15 | LinkedList reversedlinkedlist = new LinkedList<>(); 16 | 17 | linkedlist.descendingIterator().forEachRemaining(reversedlinkedlist::add); 18 | 19 | System.out.print(" to "); 20 | System.out.println(reversedlinkedlist); 21 | 22 | 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/CheckPalindromeString.java: -------------------------------------------------------------------------------- 1 | public class CheckPalindromeString { 2 | 3 | boolean checkPalindromeString(String input) { 4 | boolean result = true; 5 | int length = input.length(); 6 | for(int i=0; i < length/2; i++) { 7 | if(input.charAt(i) != input.charAt(length-i-1)) { 8 | result = false; 9 | break; 10 | } 11 | } 12 | return result; 13 | } 14 | 15 | public static void main(String[] args) { 16 | 17 | String word = "kayak"; 18 | 19 | CheckPalindromeString palindrome = new CheckPalindromeString(); 20 | 21 | System.out.print("is `"); 22 | System.out.print(word); 23 | System.out.print("` a palindrome ? "); 24 | System.out.println(palindrome.checkPalindromeString(word)); 25 | 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /src/RemoveAChar.java: -------------------------------------------------------------------------------- 1 | public class RemoveAChar { 2 | 3 | public static void main(String[] args) { 4 | 5 | String word = "GitHub"; 6 | 7 | System.out.print("word to be modified `"); 8 | System.out.print(word); 9 | System.out.print("` ..."); 10 | System.out.print(" script will remove "); 11 | 12 | String charToBeRemoved = "H"; 13 | String charToReplace = "-"; 14 | 15 | System.out.print(charToBeRemoved); 16 | System.out.print(", to be replaced by "); 17 | System.out.print(charToReplace); 18 | System.out.print(", result: `"); 19 | 20 | word = word.replace(charToBeRemoved, charToReplace); 21 | 22 | System.out.print(word); 23 | System.out.println("`"); 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /src/ShuffleArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | import java.util.Arrays; 3 | 4 | public class ShuffleArray { 5 | 6 | public static void main(String[] args) { 7 | 8 | int[] array = { 1, 2, 3, 4, 5 }; 9 | 10 | System.out.print("array to be shuffled "); 11 | System.out.println(Arrays.toString(array)); 12 | 13 | Random rand = new Random(); 14 | 15 | for (int i = 0; i < array.length; i++) { 16 | int randomIndexToSwap = rand.nextInt(array.length); 17 | int temp = array[randomIndexToSwap]; 18 | array[randomIndexToSwap] = array[i]; 19 | array[i] = temp; 20 | } 21 | 22 | System.out.print("shuffle array "); 23 | System.out.println(Arrays.toString(array)); 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /src/RemoveWhiteSpaces.java: -------------------------------------------------------------------------------- 1 | public class RemoveWhiteSpaces { 2 | 3 | String removeWhiteSpaces(String input){ 4 | StringBuilder output = new StringBuilder(); 5 | 6 | char[] charArray = input.toCharArray(); 7 | 8 | for(char c : charArray) { 9 | if (!Character.isWhitespace(c)) 10 | output.append(c); 11 | } 12 | 13 | return output.toString(); 14 | } 15 | 16 | public static void main(String[] args) { 17 | 18 | RemoveWhiteSpaces removeWhiteSpaces = new RemoveWhiteSpaces(); 19 | 20 | String word = "fdkjfkdj kjkjfd "; 21 | 22 | System.out.print("remove whitespaces from `"); 23 | System.out.print(word); 24 | System.out.print("` to no space `"); 25 | System.out.print(removeWhiteSpaces.removeWhiteSpaces("kjkfd kjfkdj ")); 26 | System.out.println("`"); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 jdevstatic 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 | -------------------------------------------------------------------------------- /src/BinarySearch.java: -------------------------------------------------------------------------------- 1 | public class BinarySearch { 2 | 3 | public static int binarySearch(int arr[], int low, int high, int key) { 4 | int mid = (low + high) / 2; 5 | 6 | while (low <= high) { 7 | if (arr[mid] < key) { 8 | low = mid + 1; 9 | } else if (arr[mid] == key) { 10 | return mid; 11 | } else { 12 | high = mid - 1; 13 | } 14 | mid = (low + high) / 2; 15 | } 16 | if (low > high) { 17 | return -1; 18 | } 19 | return -1; 20 | } 21 | 22 | public static void main(String[] args) { 23 | int intArray[] = {7, 8, 9}; 24 | 25 | int key = 8; 26 | 27 | if (binarySearch(intArray, 0, 5, key) != -1) { 28 | 29 | System.out.print("searching `"); 30 | System.out.print(key); 31 | System.out.print("` in the array... it's at index "); 32 | System.out.println(binarySearch(intArray, 0, 5, key)); 33 | 34 | 35 | } else { 36 | 37 | System.out.println("index not found"); 38 | 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/MergeSort.java: -------------------------------------------------------------------------------- 1 | public class MergeSort { 2 | 3 | public static int[] mergeTwoSortedArrays(int[] one, int[] two) { 4 | 5 | int[] sorted = new int[one.length + two.length]; 6 | 7 | int i = 0; 8 | int j = 0; 9 | int k = 0; 10 | 11 | while (i < one.length && j < two.length) { 12 | 13 | if (one[i] < two[j]) { 14 | sorted[k] = one[i]; 15 | k++; 16 | i++; 17 | } else { 18 | sorted[k] = two[j]; 19 | k++; 20 | j++; 21 | } 22 | } 23 | 24 | if (i == one.length) { 25 | 26 | while (j < two.length) { 27 | sorted[k] = two[j]; 28 | k++; 29 | j++; 30 | } 31 | } 32 | 33 | if (j == two.length) { 34 | 35 | while (i < one.length) { 36 | sorted[k] = one[i]; 37 | k++; 38 | i++; 39 | } 40 | } 41 | 42 | return sorted; 43 | 44 | } 45 | 46 | public static int[] mergeSort(int[] arr, int lo, int hi) { 47 | 48 | if (lo == hi) { 49 | int[] br = new int[1]; 50 | br[0] = arr[lo]; 51 | 52 | return br; 53 | } 54 | 55 | int mid = (lo + hi) / 2; 56 | 57 | int[] fh = mergeSort(arr, lo, mid); 58 | int[] sh = mergeSort(arr, mid + 1, hi); 59 | 60 | int[] merged = mergeTwoSortedArrays(fh, sh); 61 | 62 | return merged; 63 | } 64 | 65 | public static void main(String[] args) { 66 | 67 | int[] arr = { 70, 50, 30, 10, 20, 40, 60 }; 68 | 69 | int[] merged = mergeSort(arr, 0, arr.length - 1); 70 | 71 | for (int val : merged) { 72 | System.out.print(val + " "); 73 | } 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Coding Problems 2 | 3 | *`updated July 28, 2024`* 4 | 5 | [![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fjdevstatic%2Fjava-coding-problems&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=PAGE+VIEWS&edge_flat=false)](https://hits.seeyoufarm.com) 6 | 7 | These are the most common coding problems being asked during 8 | technical interviews, as I experienced it myself. 9 | 10 | ## Running The Code 11 | It's the convenience of Codespaces online! Simply create a Codespace, you don't need to install anything, just run using Bash. 12 | 13 | ### Steps to Run the Code 14 | 1. **Fork the Repository**: 15 | - Navigate to the repository you want to fork. 16 | - Click the "Fork" button at the top right corner of the page. 17 | 18 | 2. **Create a Codespace**: 19 | - Go to your forked repository. 20 | - Click the "Code" button. 21 | - Select "Open with Codespaces" from the dropdown menu. 22 | - If you have Codespaces enabled, you will see an interface like this: 23 | ![Codespaces](https://github.com/user-attachments/assets/580e8844-5476-4b7a-a133-0e378656096a) 24 | 25 | 3. **Navigate to the Source Directory**: 26 | - Open the terminal in Codespaces. 27 | - Change the directory to `src`: 28 | ```bash 29 | cd src 30 | ``` 31 | 32 | 4. **Run the Java File**: 33 | - Starting with Java 11, you can run Java files without compiling them first. Use the following command: 34 | 35 | ```bash 36 | java [NameOfTheFile].java 37 | ``` 38 | 39 | this is the sample interface of the online Codespaces 40 | 41 | ![Codespaces Interface](https://user-images.githubusercontent.com/47092464/182785921-838bd0e5-2707-4e08-8a0b-9127afba6866.png) 42 | 43 | enjoy! 44 | 45 | ## Source Code 46 | 1. **Binary Search** 47 | - **Problem:** Implement a binary search algorithm to find the position of a 48 | target value within a sorted array. 49 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/BinarySearch.java) 50 | - **Discussion:** Binary search is an efficient algorithm for finding an item 51 | from a sorted list of items. It works by repeatedly dividing in half the 52 | portion of the list that could contain the item, until you've narrowed down 53 | the possible locations to just one. 54 | 55 | 2. **Checking Palindrome** 56 | - **Problem:** Check if a given string is a palindrome. 57 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/CheckPalindromeString.java) 58 | - **Discussion:** A palindrome is a word, phrase, number, or other sequence 59 | of characters that reads the same forward and backward (ignoring spaces, 60 | punctuation, and capitalization). The algorithm typically involves 61 | comparing characters from the beginning and end of the string moving 62 | towards the center. 63 | 64 | 3. **Inheritance** 65 | - **Problem:** Demonstrate the concept of inheritance in Java. 66 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/tree/main/src/inheritance) 67 | - **Discussion:** Inheritance is a fundamental concept in object-oriented 68 | programming where a new class is created from an existing class. The new 69 | class (subclass) inherits attributes and methods from the existing class 70 | (superclass), allowing for code reuse and the creation of a hierarchical 71 | relationship between classes. 72 | 73 | 4. **Integer Array Sum** 74 | - **Problem:** Calculate the sum of all integers in an array. 75 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/IntegerArraySum.java) 76 | - **Discussion:** This problem involves iterating through an array of 77 | integers and accumulating the sum of its elements. It is a straightforward 78 | problem that demonstrates basic array manipulation and iteration. 79 | 80 | 5. **Merge Sort** 81 | - **Problem:** Implement the merge sort algorithm. 82 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/MergeSort.java) 83 | - **Discussion:** Merge sort is a divide-and-conquer algorithm that divides 84 | the input array into two halves, recursively sorts them, and then merges 85 | the sorted halves. It is known for its efficiency and stable sorting 86 | properties. 87 | 88 | 6. **Prime Number Checker** 89 | - **Problem:** Check if a given number is a prime number. 90 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/PrimeNumberCheck.java) 91 | - **Discussion:** A prime number is a natural number greater than 1 that has 92 | no positive divisors other than 1 and itself. The algorithm typically 93 | involves checking divisibility from 2 up to the square root of the number. 94 | 95 | 7. **Fibonacci Series** 96 | - **Problem:** Print the Fibonacci series up to a given number. 97 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/PrintFibonacciSeries.java) 98 | - **Discussion:** The Fibonacci series is a sequence where each number is the 99 | sum of the two preceding ones, usually starting with 0 and 1. This problem 100 | can be solved using iterative or recursive approaches. 101 | 102 | 8. **Remove A Character** 103 | - **Problem:** Remove all occurrences of a given character from a string. 104 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/RemoveAChar.java) 105 | - **Discussion:** This problem involves iterating through the string and 106 | building a new string that excludes the specified character. It 107 | demonstrates string manipulation techniques. 108 | 109 | 9. **Remove Whitespaces** 110 | - **Problem:** Remove all whitespaces from a string. 111 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/RemoveWhiteSpaces.java) 112 | - **Discussion:** This problem involves iterating through the string and 113 | building a new string that excludes all whitespace characters. It is a 114 | common string manipulation task. 115 | 116 | 10. **Reverse A Linked List** 117 | - **Problem:** Reverse a singly linked list. 118 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/ReverseALinkedList.java) 119 | - **Discussion:** Reversing a linked list involves changing the direction of 120 | the pointers in the list. This problem is a classic example of pointer 121 | manipulation in data structures. 122 | 123 | 11. **Reverse A String** 124 | - **Problem:** Reverse a given string. 125 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/ReverseString.java) 126 | - **Discussion:** Reversing a string involves swapping characters from the 127 | beginning and end of the string moving towards the center. It is a basic 128 | string manipulation problem. 129 | 130 | 12. **Shuffle Array** 131 | - **Problem:** Shuffle the elements of an array randomly. 132 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/ShuffleArray.java) 133 | - **Discussion:** Shuffling an array involves randomly permuting its 134 | elements. This can be achieved using algorithms like the Fisher-Yates 135 | shuffle. 136 | 137 | 13. **Sort Array** 138 | - **Problem:** Sort an array of integers. 139 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/SortArray.java) 140 | - **Discussion:** Sorting an array involves arranging its elements in a 141 | specific order (ascending or descending). Various algorithms can be used, 142 | such as quicksort, mergesort, or bubble sort. 143 | 144 | 14. **Check Vowels** 145 | - **Problem:** Check if a string contains any vowels. 146 | - **Link:** [View Code](https://github.com/jdevstatic/java-coding-problems/blob/main/src/StringContainsVowels.java) 147 | - **Discussion:** This problem involves iterating through the string and 148 | checking for the presence of vowel characters (a, e, i, o, u). It 149 | demonstrates basic string traversal and condition checking. 150 | 151 | ## License 152 | This is based on : 153 | 154 | https://www.journaldev.com/370/java-programming-interview-questions 155 | 156 | I don't know about the specific license the author used 157 | but I'm still including his link for his copyright. 158 | 159 | When you copy my work, include the author's link 160 | and my MIT license for my modifications. 161 | 162 | ## More Java Projects 163 | for more Java discussion and other details, 164 | check the Main Page -> [Java](https://github.com/jdevfullstack/java) 165 | 166 | ## More Of My Content 167 | - [jdevfullstack Profile](https://github.com/jdevfullstack) 168 | - [jdevfullstack Repos](https://github.com/jdevfullstack?tab=repositories) 169 | - [jdevfullstack Projects](https://github.com/jdevfullstack-projects) 170 | - [jdevfullstack Tutorials](https://github.com/jdevfullstack-tutorials) 171 | --------------------------------------------------------------------------------