// 1.
Two Sum
import [Link].*;
class TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < [Link]; i++) {
int diff = target - nums[i];
if ([Link](diff)) {
[Link]([Link](diff) + " " + i);
return;
[Link](nums[i], i);
// 2. Best Time to Buy and Sell Stock
class BestTimeToBuyAndSellStock {
public static void main(String[] args) {
int[] prices = {7, 1, 5, 3, 6, 4};
int min = prices[0], profit = 0;
for (int i = 1; i < [Link]; i++) {
if (prices[i] < min) min = prices[i];
else profit = [Link](profit, prices[i] - min);
[Link]("Max Profit: " + profit);
}
// 3. Rotate Array
class RotateArray {
static void reverse(int[] a, int l, int r) {
while (l < r) {
int t = a[l]; a[l++] = a[r]; a[r--] = t;
public static void main(String[] args) {
int[] a = {1, 2, 3, 4, 5, 6, 7};
int k = 3, n = [Link];
k = k % n;
reverse(a, 0, n - 1);
reverse(a, 0, k - 1);
reverse(a, k, n - 1);
for (int i : a) [Link](i + " ");
// 4. Reverse a Linked List
class ReverseLinkedList {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node head = new Node(1);
[Link] = new Node(2);
[Link] = new Node(3);
head = reverse(head);
while (head != null) {
[Link]([Link] + " ");
head = [Link];
static Node reverse(Node head) {
Node prev = null;
while (head != null) {
Node next = [Link];
[Link] = prev;
prev = head;
head = next;
return prev;
// 5. Merge Two Sorted Lists
class MergeSortedLists {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node a = new Node(1);
[Link] = new Node(3);
[Link] = new Node(5);
Node b = new Node(2);
[Link] = new Node(4);
[Link] = new Node(6);
Node result = merge(a, b);
while (result != null) {
[Link]([Link] + " ");
result = [Link];
static Node merge(Node l1, Node l2) {
Node dummy = new Node(0), tail = dummy;
while (l1 != null && l2 != null) {
if ([Link] < [Link]) {
[Link] = l1;
l1 = [Link];
} else {
[Link] = l2;
l2 = [Link];
tail = [Link];
[Link] = (l1 != null) ? l1 : l2;
return [Link];
// 6. Detect Cycle in a Linked List
class DetectCycle {
static class Node {
int data;
Node next;
Node(int d) { data = d; }
public static void main(String[] args) {
Node head = new Node(1);
[Link] = new Node(2);
[Link] = new Node(3);
[Link] = [Link]; // cycle
[Link](hasCycle(head));
static boolean hasCycle(Node head) {
Node slow = head, fast = head;
while (fast != null && [Link] != null) {
slow = [Link];
fast = [Link];
if (slow == fast) return true;
return false;
// 7. Reverse String (User input without inbuilt functions)
import [Link];
class ReverseString {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String input = [Link]();
char[] chars = [Link]();
for (int i = [Link] - 1; i >= 0; i--) {
[Link](chars[i]);
[Link]();
[Link]();
}
// 8. Valid Parentheses
import [Link].*;
class ValidParentheses {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
String s = [Link]();
Stack<Character> stack = new Stack<>();
for (char c : [Link]()) {
if (c == '(' || c == '{' || c == '[') [Link](c);
else if ([Link]() ||
(c == ')' && [Link]() != '(') ||
(c == '}' && [Link]() != '{') ||
(c == ']' && [Link]() != '[')) {
[Link](false);
return;
[Link]([Link]());
// 9. Longest Common Prefix
class LongestCommonPrefix {
public static void main(String[] args) {
String[] strs = {"flower", "flow", "flight"};
if ([Link] == 0) {
[Link]("");
return;
String prefix = strs[0];
for (int i = 1; i < [Link]; i++) {
while (!strs[i].startsWith(prefix)) {
prefix = [Link](0, [Link]() - 1);
if ([Link]()) {
[Link]("");
return;
[Link](prefix);
// 10. LRU Cache
import [Link].*;
class LRUCache {
private final int capacity;
private final LinkedHashMap<Integer, Integer> map;
public LRUCache(int capacity) {
[Link] = capacity;
[Link] = new LinkedHashMap<>(capacity, 0.75f, true) {
protected boolean removeEldestEntry([Link]<Integer, Integer> eldest) {
return size() > capacity;
};
public int get(int key) {
return [Link](key, -1);
public void put(int key, int value) {
[Link](key, value);
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(2);
[Link](1, 1);
[Link](2, 2);
[Link]([Link](1));
[Link](3, 3);
[Link]([Link](2));
// 11. Group Anagrams
import [Link].*;
class GroupAnagrams {
public static void main(String[] args) {
String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
Map<String, List<String>> map = new HashMap<>();
for (String s : strs) {
char[] chars = [Link]();
[Link](chars);
String key = new String(chars);
[Link](key, k -> new ArrayList<>()).add(s);
[Link]([Link]());
// 12. Inorder Traversal
class InorderTraversal {
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
[Link] = new TreeNode(2);
[Link] = new TreeNode(3);
inorder(root);
static void inorder(TreeNode root) {
if (root == null) return;
inorder([Link]);
[Link]([Link] + " ");
inorder([Link]);
// 13. Validate Binary Search Tree
class ValidateBST {
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) { val = x; }
public static void main(String[] args) {
TreeNode root = new TreeNode(2);
[Link] = new TreeNode(1);
[Link] = new TreeNode(3);
[Link](isValidBST(root));
static boolean isValidBST(TreeNode root) {
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
static boolean validate(TreeNode node, long min, long max) {
if (node == null) return true;
if ([Link] <= min || [Link] >= max) return false;
return validate([Link], min, [Link]) && validate([Link], [Link], max);
// 15. Coin Change
import [Link].*;
class CoinChange {
public static void main(String[] args) {
int[] coins = {1, 2, 5};
int amount = 11;
int[] dp = new int[amount + 1];
[Link](dp, amount + 1);
dp[0] = 0;
for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] = [Link](dp[i], dp[i - coin] + 1);
[Link](dp[amount] > amount ? -1 : dp[amount]);
// 17. Merge Sort
import [Link].*;
class MergeSort {
public static void main(String[] args) {
int[] arr = {5, 2, 4, 7, 1};
sort(arr, 0, [Link] - 1);
[Link]([Link](arr));
static void sort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
static void merge(int[] arr, int l, int m, int r) {
int[] left = [Link](arr, l, m + 1);
int[] right = [Link](arr, m + 1, r + 1);
int i = 0, j = 0, k = l;
while (i < [Link] && j < [Link]) {
if (left[i] <= right[j]) arr[k++] = left[i++];
else arr[k++] = right[j++];
while (i < [Link]) arr[k++] = left[i++];
while (j < [Link]) arr[k++] = right[j++];
// Java DSA + Fintech Problem Solutions
// Existing problems 1-13, 15, 17 already included.
// 14. Fibonacci Sequence
class Fibonacci {
public static void main(String[] args) {
int n = 10;
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
[Link]("Fibonacci(" + n + ") = " + dp[n]);
// 16. Binary Search
class BinarySearch {
public static void main(String[] args) {
int[] arr = {1, 3, 5, 7, 9};
int target = 7;
int low = 0, high = [Link] - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
[Link]("Found at index: " + mid);
return;
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
[Link]("Not found");
}
}
// 18. Simple and Compound Interest
class InterestCalculator {
public static void main(String[] args) {
double principal = 1000, rate = 5, time = 2;
double si = (principal * rate * time) / 100;
double ci = principal * [Link](1 + rate / 100, time) - principal;
[Link]("Simple Interest: " + si);
[Link]("Compound Interest: " + ci);
// 19. Stock Price Movement
class StockMovement {
public static void main(String[] args) {
double[] prices = {100, 105, 102, 110};
for (int i = 1; i < [Link]; i++) {
double change = ((prices[i] - prices[i - 1]) / prices[i - 1]) * 100;
[Link]("Day %d Change: %.2f%%\n", i, change);
// 20. Payment Processing Logic
class PaymentProcessor {
public static void main(String[] args) {
String type = "credit";
double amount = 1000;
double fee = 0;
switch (type) {
case "credit": fee = amount * 0.02; break;
case "debit": fee = amount * 0.01; break;
case "upi": fee = 0; break;
default: fee = amount * 0.03;
[Link]("Transaction Fee: " + fee);
// 21. Fraud Detection Logic
class FraudDetection {
public static void main(String[] args) {
double[] transactions = {200, 5000, 150, 8000, 50};
for (double t : transactions) {
if (t > 5000) {
[Link]("Flagged suspicious transaction: " + t);
// 22. Basic Trading Algorithm
class TradingBot {
public static void main(String[] args) {
int[] prices = {100, 180, 260, 310, 40, 535, 695};
for (int i = 1; i < [Link]; i++) {
if (prices[i] > prices[i - 1]) {
[Link]("Buy at " + prices[i - 1] + ", Sell at " + prices[i]);