Java के इंटरव्यू सवाल
In this page:
String Reversal
किसी string को reverse करना आमतौर पर इसे एक char[] में convert करके, दोनों ends से characters swap करके अंदर की ओर, या built-in shortcut के लिए बस new StringBuilder(str).reverse() call करके implement किया जाता है।
उदाहरण: String Reversal
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "hello"
String text = "hello";
// Print a line to the console
System.out.println(new StringBuilder(text).reverse());
}
}
Login to try C/C++/Java/PHP code in the editor
Palindrome Check
एक palindrome check किसी string को इसके अपने reverse से compare करता है, या एक साथ दोनों ends से अंदर की ओर characters compare करते हुए चलता है — वही underlying idea बस दो अलग तरीकों से implemented।
उदाहरण: Palindrome Check
// Define the class Main
public class Main {
static boolean isPalindrome(String s) {
return s.equals(new StringBuilder(s).reverse().toString());
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println(isPalindrome("racecar"));
}
}
Login to try C/C++/Java/PHP code in the editor
Finding Duplicates
किसी collection में duplicates ढूंढना आमतौर पर एक HashSet में seen elements track करके solve किया जाता है: अगर एक element इसे जोड़ने की कोशिश करते समय पहले से मौजूद हो, आपने एक duplicate ढूंढ लिया।
उदाहरण: Finding Duplicates
// Import java.util.HashSet so it can be used by its short name
import java.util.HashSet;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare nums and set it to {1, 2, 3, 2}
int[] nums = {1, 2, 3, 2};
// Create a new HashSet object and store it in seen
HashSet<Integer> seen = new HashSet<>();
for (int n : nums) {
// Check whether !seen.add(n)
if (!seen.add(n)) {
// Print a line to the console
System.out.println("Duplicate: " + n);
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
The FizzBuzz Problem
FizzBuzz — 3 के multiples के लिए Fizz print करना, 5 के लिए Buzz, और दोनों के multiples के लिए FizzBuzz — basic control flow और modulo operator test करने वाला एक classic beginner interview problem है।
उदाहरण: The FizzBuzz Problem
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Loop: repeat while the condition holds
for (int i = 1; i <= 15; i++) {
if (i % 15 == 0) System.out.println("FizzBuzz");
else if (i % 3 == 0) System.out.println("Fizz");
else if (i % 5 == 0) System.out.println("Buzz");
else System.out.println(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Counting Character Frequencies
Character frequencies count करना आमतौर पर एक HashMap<Character, Integer> उपयोग करता है, string को एक बार scan करते समय हर character का count increment करते हुए।
उदाहरण: Counting Character Frequencies
// Import java.util.HashMap so it can be used by its short name
import java.util.HashMap;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare text and set it to "hello"
String text = "hello";
// Create a new HashMap object and store it in freq
HashMap<Character, Integer> freq = new HashMap<>();
for (char c : text.toCharArray()) {
freq.merge(c, 1, Integer::sum);
}
// Print a line to the console
System.out.println(freq);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक loop में
+=से एक string reverse करना, जबStringBuilder.reverse()simpler है। - Case और spaces नज़रअंदाज़ किए बिना एक palindrome जांचना।
- 15 से पहले 3 और 5 के लिए FizzBuzz जांचना, इसलिए 15 के multiples सिर्फ
Fizzprint करते हैंFizzBuzzके बजाय।
Math, date और time, औरRandomजैसी Utility classes आम tasks support करती हैं।- Enums, annotations, और scope और lifetime language का ज़्यादा हिस्सा describe करते हैं।
- Best practices, common mistakes, और interview questions आपको जो सीखा उसे apply करने में मदद करते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: