21. Write a function to partition a list of integers such that all even numbers appear before odd numbers
Required Input:
[3, 1, 2, 4]
Expected Output:
[4, 2, 1, 3]
Code In Java
import java.util.*;
public class Main {
public static void partitionEvenOdd() {
int[] arr = {3, 1, 2, 4};
// Your code here
}
}
Output
Click Run Button to view compiled output
22. Write a function to perform bucket sort on an array
Required Input:
[0.42, 0.32, 0.53, 0.25, 0.47, 0.51]
Expected Output:
[0.25, 0.32, 0.42, 0.47, 0.51, 0.53]
Code In Java
import java.util.*;
public class Main {
public static void bucketSort() {
double[] arr = {0.42, 0.32, 0.53, 0.25, 0.47, 0.51};
// Your code here
}
}
Output
Click Run Button to view compiled output
23. Implement a function to solve the Word Search problem on a grid (given a 2D grid of characters, find if a word exists)
Required Input:
board = [['A', 'B', 'C', 'E'], ['S', 'F', 'C', 'S'], ['A', 'D', 'E', 'E']]
word = 'ABCCED'
Expected Output:
true
Code In Java
import java.util.*;
public class Main {
public static boolean exist() {
char[][] board = {
{'A', 'B', 'C', 'E'},
{'S', 'F', 'C', 'S'},
{'A', 'D', 'E', 'E'}
};
String word = "ABCCED";
// Your code here
return false;
}
}
Output
Click Run Button to view compiled output
24. Write a function to sort a linked list in O(n log n) time using constant space complexity
Required Input:
Linked list: 4 → 2 → 1 → 3
Expected Output:
1 2 3 4
Code In Java
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public class Main {
public static ListNode sortList() {
// Create the linked list here
// Your code here
return null;
}
}
Output
Click Run Button to view compiled output
25. Write a function to calculate the power of a number using a divide-and-conquer approach (exponentiation by squaring)
Required Input:
2, 10
Expected Output:
1024
Code In Java
import java.util.*;
public class Main {
public static int power() {
int base = 2, exp = 10;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
26. Implement a function to find the longest path in a matrix where each cell has to be greater than the previous cell in the path
Required Input:
matrix =
[[9, 9, 4],
[6, 6, 8],
[2, 1, 1]]
Expected Output:
4
Code In Java
import java.util.*;
public class Main {
public static int longestPath() {
int[][] matrix = {
{9, 9, 4},
{6, 6, 8},
{2, 1, 1}
};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
27. Implement a function to solve the Combination Sum problem (find all unique combinations where candidates sum to a target)
Required Input:
candidates = [2, 3, 6, 7], target = 7
Expected Output:
[[2, 2, 3], [7]]
Code In Java
import java.util.*;
public class Main {
public static List
- > combinationSum() {
int[] candidates = {2, 3, 6, 7};
int target = 7;
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
28. Write a function to generate all valid combinations of parentheses
Required Input:
n = 3
Expected Output:
[((())), (()()), (())(), ()(()), ()()()]
Code In Java
import java.util.*;
public class Main {
public static List generateParenthesis() {
int n = 3;
// Your code here
return new ArrayList<>();
}
}
Output
Click Run Button to view compiled output
29. Implement a function to find the largest rectangle in a histogram
Required Input:
[2, 1, 5, 6, 2, 3]
Expected Output:
10
Code In Java
import java.util.*;
public class Main {
public static int largestRectangleArea() {
int[] heights = {2, 1, 5, 6, 2, 3};
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output
30. Implement a function to solve the Unique Paths problem (find the number of ways to move from the top-left corner to the bottom-right corner of an m x n grid)
Required Input:
m = 3, n = 2
Expected Output:
3
Code In Java
import java.util.*;
public class Main {
public static int uniquePaths() {
int m = 3, n = 2;
// Your code here
return 0;
}
}
Output
Click Run Button to view compiled output