Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/test/java/codeWithLogicExplanation/CharacterCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codeWithLogicExplanation;

import java.util.HashMap;

public class CharacterCount {

public static void main(String[] args) {

// Input string
String str = "masum";

// HashMap to store character as key and its count as value
HashMap<Character, Integer> map = new HashMap<>();

// Loop through each character in the string
for (char c : str.toCharArray()) {
// getOrDefault(c, 0) returns the current count if present, otherwise 0
// Then we add 1 to update the count
map.put(c, map.getOrDefault(c, 0) + 1);
}

// Print the final map showing character counts
System.out.println(map);
}
}
30 changes: 30 additions & 0 deletions src/test/java/codeWithLogicExplanation/CharacterCounts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codeWithLogicExplanation;

import java.util.HashMap;

public class CharacterCounts {
public static void main(String[] args) {

// Input string
String str = "masum";

// HashMap to store character as key and its frequency as value
HashMap<Character, Integer> map = new HashMap<>();

// Loop through each character in the string
for (char c : str.toCharArray()) {

// Check if the character already exists in the map
if (map.containsKey(c)) {
// If yes, increment its count
map.put(c, map.get(c) + 1);
} else {
// If not, add it with initial count = 1
map.put(c, 1);
}
}

// Print the final map showing character counts
System.out.println(map);
}
}
22 changes: 22 additions & 0 deletions src/test/java/codeWithLogicExplanation/ExpandString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package codeWithLogicExplanation;

public class ExpandString {
public static void main(String[] args) {
String str = "a2b3c4"; // input string with characters followed by numbers
String result = ""; // final expanded string

// Loop through the string in steps of 2 (character + digit)
for (int i = 0; i < str.length(); i += 2) {
char ch = str.charAt(i); // the character
int count = Character.getNumericValue(str.charAt(i + 1)); // the number after character

// Repeat the character 'count' times
for (int j = 1; j <= count; j++) {
result = result + ch;
}
}

// Print the expanded string
System.out.println(result);
}
}
30 changes: 30 additions & 0 deletions src/test/java/codeWithLogicExplanation/PrimeChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codeWithLogicExplanation;

public class PrimeChecker {

public static void main(String[] args) {
// Loop through numbers from 1 to 100
for (int num = 1; num <= 100; num++) {
// Call isPrime() and print result using ternary operator
System.out.println(num + " is " + (isPrime(num) ? "Prime" : "Not Prime"));
}
}

// Method to check if a number is prime
private static boolean isPrime(int num) {

// Prime numbers are greater than 1
if (num <= 1)
return false;

// Check divisibility from 2 up to sqrt(num)
// If divisible by any number in this range, it's not prime
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return false;
}

// If no divisors found, it's prime
return true;
}
}
31 changes: 31 additions & 0 deletions src/test/java/codeWithLogicExplanation/ReveseSentnece.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package codeWithLogicExplanation;

public class ReveseSentnece {
public static void main(String[] args) {
// Input string where each word is reversed
String str = "ihled si latipac fo aidnI";

// Split the sentence into words using space as delimiter
String words[] = str.split(" ");

// Variable to store the final reversed sentence
String revSen = "";

// Loop through each word
for (String word : words) {
String revWord = ""; // to store reversed word
int len = word.length();

// Reverse the current word character by character
for (int i = len - 1; i >= 0; i--) {
revWord = revWord + word.charAt(i);
}

// Add the reversed word to the sentence
revSen = revSen + revWord + " ";
}

// Print the final reversed sentence (trim removes extra space at the end)
System.out.println(revSen.trim());
}
}
28 changes: 28 additions & 0 deletions src/test/java/codeWithLogicExplanation/SecondHigherNumInArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codeWithLogicExplanation;

public class SecondHigherNumInArray {

public static void main(String[] args) {

int arr[] = { 112, 34, 553, 35, 23, 678, 876, 366, 53, 757, 3425, 4444 };

int first = Integer.MIN_VALUE; // highest number
int second = Integer.MIN_VALUE; // second highest number

// Loop through each number in the array
for (int num : arr) {
if (num > first) {
// If current number is greater than first,
// then update second as old first, and first as current number
second = first;
first = num;
} else if (num > second && num != first) {
// If current number is greater than second but not equal to first,
// update second
second = num;
}
}

System.out.println("Second highest number is : " + second);
}
}
26 changes: 26 additions & 0 deletions src/test/java/codeWithLogicExplanation/StringCompresssed.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codeWithLogicExplanation;

import java.util.LinkedHashMap;
import java.util.Map;

public class StringCompresssed {
public static void main(String[] args) {
String str = "aaaaaaggggg"; // input string
String result = "";

// LinkedHashMap preserves insertion order
Map<Character, Integer> charc = new LinkedHashMap<>();

// Count frequency of each character
for (char c : str.toCharArray()) {
charc.put(c, charc.getOrDefault(c, 0) + 1);
}

// Build compressed string
for (Map.Entry<Character, Integer> entry : charc.entrySet()) {
result = result + entry.getKey() + entry.getValue();
}

System.out.println(result);
}
}
30 changes: 30 additions & 0 deletions src/test/java/codeWithLogicExplanation/VowelLetterCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package codeWithLogicExplanation;

public class VowelLetterCount {

public static void main(String[] args) {

// Input string
String input = "rajuiUmbrela";

// String containing all vowels (both uppercase and lowercase)
String vowel = "AEIOUaeiou";

// Counter to keep track of vowels
int count = 0;

// Loop through each character of the input string
for (char c : input.toCharArray()) {

// Check if the character exists in the vowel string
// indexOf(c) returns -1 if the character is not found
if (vowel.indexOf(c) != -1) {
count++; // Increase count if vowel found
System.out.print(c+" "); // Print the vowel character
}
}

// Print total number of vowels found
System.out.println("\ntotal number of vowels found: "+count);
}
}
31 changes: 31 additions & 0 deletions src/test/java/codeWithLogicExplanation/WhiteSpaceAndWordCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package codeWithLogicExplanation;

public class WhiteSpaceAndWordCount {
public static void main(String[] args) {
String str = "hell aa ad ddsa ddd dd";

// Count spaces
int count = 0;
for (int i = 0; i <= str.length() - 1; i++) {
if (str.charAt(i) == ' ') {
count++;
}
}
System.out.println("Total white space count is : " + count);

// Count words
int wordCount = 0;
for (int j = 0; j <= str.length() - 2; j++) { // avoid index out of bounds
// If current char is space AND next char is not space,
// it means a word has ended
if (str.charAt(j) == ' ' && str.charAt(j + 1) != ' ') {
wordCount++;
}
}

// Add 1 to count the first word (since loop only counts transitions)
wordCount++;

System.out.println("Total word Count is : " + wordCount);
}
}
25 changes: 25 additions & 0 deletions src/test/java/codeWithLogicExplanation/WhiteSpaceCount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package codeWithLogicExplanation;

public class WhiteSpaceCount {

public static void main(String[] args) {

// Input string with spaces
String input = "India is a big country";

// Counter to keep track of spaces
int count = 0;

// Loop through each character in the string
for (int i = 0; i <= input.length() - 1; i++) {

// Check if the current character is a space
if (input.charAt(i) == ' ') {
count++; // Increase count if space found
}
}

// Print total number of spaces
System.out.println(count);
}
}
35 changes: 35 additions & 0 deletions src/test/java/codeWithLogicExplanation/WordCountExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package codeWithLogicExplanation;

import java.util.HashMap;

public class WordCountExample {

public static void main(String[] args) {

// Input sentence
String sentence = "Interview with aditya CGO Interview";

// Convert to lowercase and split by spaces
String[] words = sentence.toLowerCase().split(" ");

// HashMap to store word as key and frequency as value
HashMap<String, Integer> wordCount = new HashMap<>();

// Loop through each word
for (String word : words) {
// Check if the word already exists in the map
if (wordCount.containsKey(word)) {
// If yes, increment its count
wordCount.put(word, wordCount.get(word) + 1);
} else {
// If not, add it with initial count = 1
wordCount.put(word, 1);
}
}

// Print each word and its frequency
for (String word : wordCount.keySet()) {
System.out.println(word + ": " + wordCount.get(word));
}
}
}
26 changes: 26 additions & 0 deletions src/test/java/codeWithLogicExplanation/WordDublicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codeWithLogicExplanation;

import java.util.HashSet;
import java.util.Set;

public class WordDublicate {
public static void main(String[] args) {
String Str = "hello ram delhi patna bihar delhi patna bihar";

// Split the sentence into words
String[] words = Str.split(" ");

// HashSet to store unique words
Set<String> uniq = new HashSet<>();

// Add each word to the set
for (String word : words) {
uniq.add(word.trim()); // trim removes any accidental spaces
}

// Print unique words
for (String word : uniq) {
System.out.println(word);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Sun Mar 01 00:25:16 IST 2026
#Sun Mar 01 00:47:00 IST 2026
artifactId=interviewPractice2026
groupId=com.interviewPractice2026
m2e.projectLocation=D\:\\My_Project\\Testing_Project\\interviewPractice2026
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.