Here are some Java-specific examples and explanations to help you prepare for the
written coding test. The focus will be on data structures, algorithms, and basic
programming concepts in Java.
### Sample Questions and Solutions in Java
#### Data Structures
1. **Arrays**:
- **Find the Maximum Subarray Sum (Kadane’s Algorithm)**:
```java
public class MaxSubarraySum {
public static int maxSubArray(int[] nums) {
int maxSoFar = nums[0], maxEndingHere = nums[0];
for (int i = 1; i < nums.length; i++) {
maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]);
maxSoFar = Math.max(maxSoFar, maxEndingHere);
}
return maxSoFar;
}
}
```
2. **Linked Lists**:
- **Reverse a Linked List**:
```java
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class ReverseLinkedList {
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
```
3. **Stacks and Queues**:
- **Implement a Stack Using Two Queues**:
```java
import java.util.LinkedList;
import java.util.Queue;
public class MyStack {
private Queue<Integer> queue1 = new LinkedList<>();
private Queue<Integer> queue2 = new LinkedList<>();
public void push(int x) {
queue2.add(x);
while (!queue1.isEmpty()) {
queue2.add(queue1.remove());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue1.remove();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
```
4. **Trees**:
- **Inorder Traversal of a Binary Tree**:
```java
import java.util.ArrayList;
import java.util.List;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
}
public class InorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderHelper(root, result);
return result;
}
private void inorderHelper(TreeNode root, List<Integer> result) {
if (root != null) {
inorderHelper(root.left, result);
result.add(root.val);
inorderHelper(root.right, result);
}
}
}
```
#### Algorithms
1. **Sorting and Searching**:
- **Implement QuickSort**:
```java
public class QuickSort {
public void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
private int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}
}
```
2. **Dynamic Programming**:
- **Find the Longest Common Subsequence**:
```java
public class LongestCommonSubsequence {
public int lcs(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int[][] dp = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[m][n];
}
}
```
3. **Greedy Algorithms**:
- **Find the Minimum Number of Coins for a Given Amount**:
```java
import java.util.Arrays;
public class CoinChange {
public int coinChange(int[] coins, int amount) {
Arrays.sort(coins);
int count = 0;
for (int i = coins.length - 1; i >= 0; i--) {
if (amount >= coins[i]) {
count += amount / coins[i];
amount %= coins[i];
}
}
return amount == 0 ? count : -1;
}
}
```
#### Basic Programming Concepts
1. **String Manipulation**:
- **Reverse the Words in a Sentence**:
```java
public class ReverseWords {
public String reverseWords(String s) {
String[] words = s.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = words.length - 1; i >= 0; i--) {
sb.append(words[i]);
if (i != 0) {
sb.append(" ");
}
}
return sb.toString();
}
}
```
2. **Bit Manipulation**:
- **Count the Number of 1 Bits in an Integer**:
```java
public class BitCount {
public int hammingWeight(int n) {
int count = 0;
while (n != 0) {
count += n & 1;
n >>= 1;
}
return count;
}
}
```
3. **Mathematics**:
- **Find the GCD of Two Numbers**:
```java
public class GCD {
public int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
}
```
### Preparation Tips
- **Practice Regularly**: Use coding platforms like LeetCode, HackerRank, and
CodeSignal to practice solving problems in Java.
- **Understand Concepts**: Focus on understanding the underlying concepts of data
structures and algorithms rather than just memorizing solutions.
- **Mock Tests**: Simulate test conditions by taking mock coding tests within a
time limit.
- **Read Java Documentation**: Familiarize yourself with Java's built-in libraries
and functions to make coding more efficient.
By practicing these types of questions and focusing on the key concepts, you'll be
well-prepared for the written coding test at HashedIn.