0% found this document useful (0 votes)
4 views

Codeing Interview Questions

Uploaded by

Veeresh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Codeing Interview Questions

Uploaded by

Veeresh Patil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

public class StringReverse {


public static void main(String[] args) {
String str = "manjunatha";

// Reverse the string using streams


String reversed = str.chars()
.mapToObj(x -> String.valueOf((char) x))
.reduce("", (s1, s2) -> s2 + s1);

// Print the reversed string


System.out.println("Original string: " + str);
System.out.println("Reversed string: " + reversed);
}
}
2.public class WordCount {
public static void main(String[] args) {
String str = "Big black bug bit a big black dog on his big nose";

// Count the occurrences of each word (case insensitive)


Map<String, Long> map = Arrays.stream(str.split(" "))
.collect(Collectors.groupingBy(String::toLowerCase,
Collectors.counting()));

// Print the result


System.out.println(map);
}
}
3.public class ReverseWords {
public static void main(String[] args) {
String str = "Big black bug bit a big black dog on his big nose";

// Reverse each word in the string


String reversedWords = Arrays.stream(str.split(" "))
.map(word -> new StringBuilder(word).reverse().toString())
.collect(Collectors.joining(" "));

// Print the result


System.out.println(reversedWords);
}
}
4.public class IntegerToStringList {
public static void main(String[] args) {
// Create an ArrayList of Integers
List<Integer> integerList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));

// Convert Integer List to String List


List<String> stringList = integerList.stream()
.map(String::valueOf) // Convert each Integer to String
.collect(Collectors.toList()); // Collect results into a List

// Print the String List


System.out.println("String List: " + stringList);
}
}
5.find Maximum & min character Occur in given String
public class MaxCharacterOccurrence {
public static void main(String[] args) {
String str = "Hello World";
// Find the character with maximum occurrences
char maxChar = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse('\0');

// Find the character with minimum occurrences


char minChar = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet().stream()
.min(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse('\0');

// Print results
System.out.println("Character with maximum occurrence: " + maxChar);
System.out.println("Character with minimum occurrence: " + minChar);
}
}
6.find first non repeat character in string
public class FirstNonRepeatCharacter {
public static void main(String[] args) {
String str = "Hello Word";

// Find the first non-repeating character


char firstNonRepeatChar = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() == 1) // Check for count of 1
.map(Map.Entry::getKey)
.findFirst()
.orElse('\0'); // Default value if none found

// Print the result


System.out.println("First non-repeating character: " + firstNonRepeatChar);
}
}
7.find first repeating character in string
public class FirstRepeatingCharacter {
public static void main(String[] args) {
String str = "Hello Word";

// Find the first repeating character


char firstRepeatingChar = str.chars()
.mapToObj(c -> (char) c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()))
.entrySet().stream()
.filter(entry -> entry.getValue() > 1) // Check for count greater than
1
.map(Map.Entry::getKey)
.findFirst()
.orElse('\0'); // Default value if none found

// Print the result


System.out.println("First repeating character: " + firstRepeatingChar);
}
}
8.flattenedList
public class FlattenNestedList {
public static void main(String[] args) {
// Create a nested list of integers
List<List<Integer>> nestedList = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
);

// Flatten the nested list


List<Integer> flattenedList = nestedList.stream()
.flatMap(List::stream)
.collect(Collectors.toList());

// Print the flattened list


System.out.println(flattenedList); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
9.reverseArrsyList
public class ReverseArrayList {
public static void main(String[] args) {
// Create a List of Strings
List<String> list = Arrays.asList("1", "2", "3", "4", "5");

// Create an ArrayList to hold the reversed elements


ArrayList<String> reversed = new ArrayList<>(list.size());

// Reverse the list using IntStream


IntStream.range(0, list.size())
.mapToObj(i -> list.get(list.size() - i - 1))
.forEach(reversed::add);

// Print the reversed list


System.out.println("Reversed List: " + reversed);
}
}
10.find second maximum
public class SecondMaximum {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(11, 9, 23, 9, 5);

// Find the second maximum value


int secondMax = list.stream()
.distinct() // Remove duplicates
.sorted(Collections.reverseOrder()) // Sort in
descending order
.skip(1) // Skip the first maximum
.findFirst() // Get the next element
.orElseThrow(() -> new IllegalArgumentException("No
second maximum found"));
// Print the result
System.out.println("Second maximum: " + secondMax);
}
}

You might also like