← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 10 of 10

Java के इंटरव्यू सवाल

Interview questions programming jobs के लिए apply करते समय पूछे जाने वाले classic puzzles हैं, जैसे एक word reverse करना या FizzBuzz। उन्हें practice करना एक बड़े game से पहले rehearse करने जैसा है।

String Reversal

किसी string को reverse करना आमतौर पर इसे एक char[] में convert करके, दोनों ends से characters swap करके अंदर की ओर, या built-in shortcut के लिए बस new StringBuilder(str).reverse() call करके implement किया जाता है।

उदाहरण: String Reversal

java
// 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());
	}
}

Palindrome Check

एक palindrome check किसी string को इसके अपने reverse से compare करता है, या एक साथ दोनों ends से अंदर की ओर characters compare करते हुए चलता है — वही underlying idea बस दो अलग तरीकों से implemented।

उदाहरण: Palindrome Check

java
// 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"));
	}
}

Finding Duplicates

किसी collection में duplicates ढूंढना आमतौर पर एक HashSet में seen elements track करके solve किया जाता है: अगर एक element इसे जोड़ने की कोशिश करते समय पहले से मौजूद हो, आपने एक duplicate ढूंढ लिया।

उदाहरण: Finding Duplicates

java
// 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);
			}
		}
	}
}

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

java
// 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);
		}
	}
}

Counting Character Frequencies

Character frequencies count करना आमतौर पर एक HashMap<Character, Integer> उपयोग करता है, string को एक बार scan करते समय हर character का count increment करते हुए।

उदाहरण: Counting Character Frequencies

java
// 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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक loop में += से एक string reverse करना, जब StringBuilder.reverse() simpler है।
  2. Case और spaces नज़रअंदाज़ किए बिना एक palindrome जांचना।
  3. 15 से पहले 3 और 5 के लिए FizzBuzz जांचना, इसलिए 15 के multiples सिर्फ Fizz print करते हैं FizzBuzz के बजाय।
चैप्टर सारांश
  • Math, date और time, और Random जैसी Utility classes आम tasks support करती हैं।
  • Enums, annotations, और scope और lifetime language का ज़्यादा हिस्सा describe करते हैं।
  • Best practices, common mistakes, और interview questions आपको जो सीखा उसे apply करने में मदद करते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.