0% found this document useful (0 votes)
3 views4 pages

AMCAT 50 Advanced Java Coding Questions FULL

The document contains 50 advanced Java coding questions along with their solutions. It includes various algorithms and data structure problems such as checking for palindromes, counting connected components in a binary matrix, and finding unique elements. Each solution is provided with code snippets demonstrating the implementation in Java.

Uploaded by

bharatiyaboy1267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

AMCAT 50 Advanced Java Coding Questions FULL

The document contains 50 advanced Java coding questions along with their solutions. It includes various algorithms and data structure problems such as checking for palindromes, counting connected components in a binary matrix, and finding unique elements. Each solution is provided with code snippets demonstrating the implementation in Java.

Uploaded by

bharatiyaboy1267
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AMCAT Automata - 50 Advanced Java Coding Questions with Solutions

Q1. Check if a list is a palindrome


public static boolean isListPalindrome(List<Integer> list) {
int i = 0, j = list.size() - 1;
while (i < j) {
if (!list.get(i).equals(list.get(j))) return false;
i++; j--;
}
return true;
}

Q2. Count connected 1s in a binary matrix (DFS)


static int[] dx = {-1, 0, 1, 0};
static int[] dy = {0, 1, 0, -1};
public static int countGroups(int[][] grid) {
int count = 0;
boolean[][] vis = new boolean[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1 && !vis[i][j]) {
dfs(grid, vis, i, j);
count++;
}
}
}
return count;
}
static void dfs(int[][] grid, boolean[][] vis, int x, int y) {
int n = grid.length, m = grid[0].length;
vis[x][y] = true;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (nx >= 0 && ny >= 0 && nx < n && ny < m &&
grid[nx][ny] == 1 && !vis[nx][ny]) {
dfs(grid, vis, nx, ny);
}
}
}

Q3. Find unique element (every other occurs twice)


public static int findUnique(List<Integer> list) {
int res = 0;
for (int num : list) res ^= num;
return res;
}

Q4. Longest consecutive sequence in a list


public static int longestConsec(List<Integer> list) {
Set<Integer> set = new HashSet<>(list);
int maxLen = 0;
for (int num : list) {
if (!set.contains(num - 1)) {
int current = num, length = 1;
while (set.contains(current + 1)) {
current++;
length++;
}
maxLen = Math.max(maxLen, length);
}
}
return maxLen;
}

Q5. First non-repeating element


public static int firstNonRepeating(List<Integer> list) {
Map<Integer, Integer> freq = new LinkedHashMap<>();
for (int num : list) freq.put(num, freq.getOrDefault(num, 0) + 1);
for (Map.Entry<Integer, Integer> e : freq.entrySet())
if (e.getValue() == 1) return e.getKey();
return -1;
}

Q6. Print numbers in spiral matrix


public static void spiralMatrix(int n) {
int[][] mat = new int[n][n];
int val = 1, top = 0, left = 0, bottom = n - 1, right = n - 1;
while (val <= n * n) {
for (int i = left; i <= right; i++) mat[top][i] = val++;
top++;
for (int i = top; i <= bottom; i++) mat[i][right] = val++;
right--;
for (int i = right; i >= left; i--) mat[bottom][i] = val++;
bottom--;
for (int i = bottom; i >= top; i--) mat[i][left] = val++;
left++;
}
for (int[] row : mat) {
for (int x : row) System.out.print(x + " ");
System.out.println();
}
}

Q7. Check if two strings are anagrams


public static boolean isAnagram(String a, String b) {
if (a.length() != b.length()) return false;
int[] freq = new int[26];
for (char c : a.toCharArray()) freq[c - 'a']++;
for (char c : b.toCharArray()) if (--freq[c - 'a'] < 0) return false;
return true;
}

Q8. Count vowels, consonants, digits in a string


public static void countAll(String s) {
int digits = 0, vowels = 0, consonants = 0;
s = s.toLowerCase();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) digits++;
else if ("aeiou".indexOf(c) >= 0) vowels++;
else if (Character.isLetter(c)) consonants++;
}
System.out.println("Digits: " + digits);
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}

Q9. Sort array by frequency (high freq first)


public static void sortByFreq(int[] arr) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) freq.put(num, freq.getOrDefault(num, 0) + 1);
Arrays.sort(arr, (a, b) -> {
int f1 = freq.get(a), f2 = freq.get(b);
return f1 == f2 ? a - b : f2 - f1;
});
}

Q10. Find all pairs with sum K


public static void findPairs(int[] arr, int k) {
Set<Integer> set = new HashSet<>();
for (int num : arr) {
if (set.contains(k - num))
System.out.println(num + ", " + (k - num));
set.add(num);
}
}

You might also like