Coding Interview Questions and Answers in Java and Python

Here’s🚀 Coding Interview Questions + Answers (Part 2) (Java 🧑💻 Python | Medium Level) If you can solve these, you're already ahead of 70% candidates 👇 --- 🔹 11. Second Largest Element Java: int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE; for(int n : arr){ if(n > first){ second = first; first = n; } else if(n > second && n != first){ second = n; } } Python: arr = [1,5,3,9,7] print(sorted(set(arr))[-2]) --- 🔹 12. Remove Duplicates Java: Set<Integer> set = new LinkedHashSet<>(list); Python: list(set(arr)) --- 🔹 13. Missing Number (1–n) Java: int sum = n*(n+1)/2; int actual = Arrays.stream(arr).sum(); System.out.println(sum - actual); Python: n = 5 arr = [1,2,3,5] print(n*(n+1)//2 - sum(arr)) --- 🔹 14. Merge Sorted Arrays Java: int i=0,j=0; while(i<a.length && j<b.length){ if(a[i]<b[j]) System.out.print(a[i++]); else System.out.print(b[j++]); } Python: a=[1,3,5]; b=[2,4,6] print(sorted(a+b)) --- 🔹 15. Character Frequency Java: Map<Character,Integer> map = new HashMap<>(); for(char c: str.toCharArray()) map.put(c, map.getOrDefault(c,0)+1); Python: from collections import Counter print(Counter("test")) --- 🔹 16. Anagram Check Java: char[] a = s1.toCharArray(); char[] b = s2.toCharArray(); Arrays.sort(a); Arrays.sort(b); System.out.println(Arrays.equals(a,b)); Python: print(sorted(s1)==sorted(s2)) --- 🔹 17. Move Zeros to End Java: int j=0; for(int i=0;i<arr.length;i++) if(arr[i]!=0) arr[j++]=arr[i]; Python: arr = [0,1,0,3] res = [x for x in arr if x!=0] + [0]*arr.count(0) --- 🔹 18. First Non-Repeating Character Java: for(char c: str.toCharArray()) if(str.indexOf(c)==str.lastIndexOf(c)) System.out.println(c); Python: for c in s: if s.count(c)==1: print(c); break --- 🔹 19. Sort Without Inbuilt Java (Bubble Sort): for(int i=0;i<n;i++) for(int j=0;j<n-i-1;j++) if(arr[j]>arr[j+1]){ int t=arr[j]; arr[j]=arr[j+1]; arr[j+1]=t; } Python: for i in range(len(arr)): for j in range(len(arr)-i-1): if arr[j] > arr[j+1]: arr[j],arr[j+1] = arr[j+1],arr[j] --- 🔹 20. Common Elements Java: Set<Integer> set = new HashSet<>(); for(int n : arr1) set.add(n); for(int n : arr2) if(set.contains(n)) System.out.println(n); Python: print(set(a) & set(b)) --- 💡 Pro Tip: 👉 These are pattern-based questions 👉 Master them once → reuse in multiple interviews --- 🎯 Next: Part 3 (Advanced 🔥) Comment “PART 3” Follow Sri Harish Chintha for more information Watsup channel… https://lnkd.in/grR24xHU Instagram : https://lnkd.in/gdm-2PuD #Coding #Java #Python #InterviewPrep #Developers #LeetCode #SDE

To view or add a comment, sign in

Explore content categories