8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/uiDesigner.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java 8 Interview Sample Coding Questions [Solution Improvement in progress]
2 |
3 | This repository contains sample Java 8 coding questions that can be used for interview preparation. Each question focuses on a specific programming concept or problem-solving technique using Java 8 features.
4 |
5 | ## List of Questions
6 |
7 | 1. **Separate odd and even numbers in a list of integers**
8 |
9 | Given a list of integers, write a Java 8 program to separate the odd and even numbers into two separate lists.
10 |
11 | 2. **Remove duplicate elements from a list using Java 8 streams**
12 |
13 | Write a Java 8 program to remove duplicate elements from a list using the stream API and lambda expressions.
14 |
15 | 3. **Find the frequency of each character in a string using Java 8 streams**
16 |
17 | Write a Java 8 program to find the frequency of each character in a given string using the stream API and collectors.
18 |
19 | 4. **Find the frequency of each element in an array or a list**
20 |
21 | Write a Java 8 program to find the frequency of each element in an array or a list using streams and collectors.
22 |
23 | 5. **Sort a given list of decimals in reverse order**
24 |
25 | Write a Java 8 program to sort a given list of decimal numbers in reverse order.
26 |
27 | 6. **Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter**
28 |
29 | Given a list of strings, write a Java 8 program to join the strings with '[' as a prefix, ']' as a suffix, and ',' as a delimiter.
30 |
31 | 7. **Print the numbers from a given list of integers that are multiples of 5**
32 |
33 | Write a Java 8 program to print the numbers from a given list of integers that are multiples of 5.
34 |
35 | 8. **Find the maximum and minimum of a list of integers**
36 |
37 | Given a list of integers, write a Java 8 program to find the maximum and minimum numbers in the list.
38 |
39 | 9. **Merge two unsorted arrays into a single sorted array using Java 8 streams**
40 |
41 | Write a Java 8 program to merge two unsorted arrays into a single sorted array using the stream API.
42 |
43 | 10. **Merge two unsorted arrays into a single sorted array without duplicates**
44 |
45 | Write a Java 8 program to merge two unsorted arrays into a single sorted array without duplicates.
46 | 11. **Get the three maximum and three minimum numbers from a given list of integers**
47 |
48 | Write a Java 8 program to get the three maximum and three minimum numbers from a given list of integers.
49 |
50 | 12. **Check if two strings are anagrams or not using Java 8 streams**
51 |
52 | Write a Java 8 program to check if two strings are anagrams or not using the stream API and lambda expressions.
53 |
54 | 13. **Find the sum of all digits of a number in Java 8**
55 |
56 | Write a Java 8 program to find the sum of all digits of a given number.
57 |
58 | 14. **Find the second largest number in an integer array**
59 |
60 | Write a Java 8 program to find the second largest number in an integer array.
61 |
62 | 15. **Sort a list of strings according to the increasing order of their length**
63 |
64 | Write a Java 8 program to sort a given list of strings according to the increasing order of their length.
65 |
66 | 16. **Find the sum and average of all elements in an integer array**
67 |
68 | Write a Java 8 program to find the sum and average of all elements in an integer array.
69 |
70 | 17. **Find the common elements between two arrays**
71 |
72 | Write a Java 8 program to find the common elements between two arrays using streams.
73 |
74 | 18. **Reverse each word of a string using Java 8 streams**
75 |
76 | Write a Java 8 program to reverse each word of a given string using the stream API and lambda expressions.
77 |
78 | 19. **Find the sum of the first 10 natural numbers**
79 |
80 | Write a Java 8 program to find the sum of the first 10 natural numbers using streams.
81 |
82 | 20. **Reverse an integer array**
83 |
84 | Write a Java 8 program to reverse an integer array.
85 |
86 | 21. **Print the first 10 even numbers**
87 |
88 | Write a Java 8 program to print the first 10 even numbers.
89 |
90 | 22. **Find the most repeated element in an array**
91 |
92 | Write a Java 8 program to find the most repeated element in an array.
93 |
94 | 23. **Check if a string is a palindrome using Java 8 streams**
95 |
96 | Write a Java 8 program to check if a given string is a palindrome using the stream API and lambda expressions.
97 |
98 | 24. **Find strings in a list that start with a number**
99 |
100 | Given a list of strings, write a Java 8 program to find the strings that start with a number.
101 |
102 | 25. **Extract duplicate elements from an array**
103 |
104 | Write a Java 8 program to extract duplicate elements from an array.
105 |
106 | 26. **Print duplicate characters in a string**
107 |
108 | Write a Java 8 program to print the duplicate characters in a string.
109 |
110 | 27. **Find the first repeated character in a string**
111 |
112 | Write a Java 8 program to find the first repeated character in a string.
113 |
114 | 28. **Find the first non-repeated character in a string**
115 |
116 | Write a Java 8 program to find the first non-repeated character in a string.
117 |
118 | 29. **Generate the Fibonacci series**
119 |
120 | Write a Java 8 program to generate the Fibonacci series.
121 |
122 | 30. **Print the first 10 odd numbers**
123 |
124 | Write a Java 8 program to print the first 10 odd numbers.
125 |
126 | 31. **Get the last element of an array**
127 |
128 | Write a Java 8 program to get the last element of an array.
129 |
130 | 32. **Calculate the age of a person in years**
131 |
132 | Write a Java 8 program to calculate the age of a person in years given their birthday.
133 |
--------------------------------------------------------------------------------
/java8InterviewCQ.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/com/rohit/Sample.java:
--------------------------------------------------------------------------------
1 | package com.rohit;
2 |
3 | import java.time.LocalDate;
4 | import java.time.Period;
5 | import java.util.*;
6 | import java.util.function.Function;
7 | import java.util.stream.IntStream;
8 | import java.util.stream.Stream;
9 |
10 | import static java.util.Comparator.reverseOrder;
11 | import static java.util.List.of;
12 | import static java.util.stream.Collectors.*;
13 |
14 | public class Sample {
15 | public static void main(String[] args) {
16 |
17 | /**
18 | * Separate odd and even numbers in a list of integers.
19 | *
20 | * Given a list of integers, write a Java 8 program to separate
21 | * the odd and even numbers into two separate lists.
22 | */
23 |
24 | separationOfEvenOddNumberInMap();
25 | separationOfEvenOddNumberInList();
26 |
27 | /**
28 | * Remove duplicate elements from a list using Java 8 streams
29 | *
30 | * Write a Java 8 program to remove duplicate elements from a list
31 | * using the stream API and lambda expressions.
32 | */
33 |
34 | removeDuplicateFromList();
35 |
36 |
37 | /**
38 | * Find the frequency of each character in a string using Java 8 streams
39 | *
40 | * Write a Java 8 program to find the frequency of each character in
41 | * a given string using the stream API and collectors.
42 | */
43 | characterFrequency();
44 |
45 | /**
46 | * Find the frequency of each element in an array or a list
47 | *
48 | * Write a Java 8 program to find the frequency of
49 | * each element in an array or a list using streams and collectors.
50 | */
51 |
52 | wordFrequency();
53 |
54 | /**
55 | * Sort a given list of decimals in reverse order
56 | *
57 | * Write a Java 8 program to sort a given list of decimal numbers in reverse order.
58 | */
59 |
60 | reverseSortedList();
61 |
62 | /**
63 | * Join a list of strings with '[' as prefix, ']' as suffix, and ',' as delimiter
64 | *
65 | * Given a list of strings, write a Java 8 program to join the strings
66 | * with '[' as a prefix, ']' as a suffix, and ',' as a delimiter.
67 | */
68 |
69 | joinListOfStrings();
70 |
71 | /**
72 | * Print the numbers from a given list of integers that are multiples of 5
73 | *
74 | * Write a Java 8 program to print the numbers from a given list of integers that are multiples of 5.
75 | */
76 |
77 | multipleOf5();
78 |
79 | /**
80 | * Find the maximum and minimum of a list of integers
81 | * Given a list of integers, write a Java 8 program to find the maximum and minimum numbers in the list.
82 | */
83 |
84 | minMaxFromList();
85 |
86 | /**
87 | * Merge two unsorted arrays into a single sorted array using Java 8 streams
88 | * Write a Java 8 program to merge two unsorted arrays into a single-sorted array using the stream API.
89 | */
90 | mergeUnsortedArrayIntoSorted();
91 |
92 | /**
93 | * Merge two unsorted arrays into a single sorted array without duplicates
94 | * Write a Java 8 program to merge two unsorted arrays into a single-sorted array without duplicates.
95 | */
96 | mergeUnsortedArrayIntoSortedWithoutDuplicate();
97 |
98 | /**
99 | * Get the three maximum and three minimum numbers from a given list of integers
100 | *
101 | * Write a Java 8 program to get the three maximum and three minimum numbers from a given list of integers.
102 | */
103 |
104 | min3max3();
105 |
106 | /**
107 | * Check if two strings are anagrams or not using Java 8 streams
108 | * Write a Java 8 program to check if two strings are anagrams or not using the stream API and lambda expressions.
109 | */
110 |
111 | isAnagram();
112 | isAnagram2();
113 |
114 | /**
115 | * Find the sum of all digits of a number in Java 8
116 | *
117 | * Write a Java 8 program to find the sum of all digits of a given number.
118 | *
119 | */
120 | sumOf();
121 | /**
122 | * Find the second-largest number in an integer array
123 | *
124 | * Write a Java 8 program to find the second-largest number in an integer array.
125 | */
126 | secondLargestNumberFromList();
127 |
128 |
129 | /**
130 | * Sort a list of strings according to the increasing order of their length
131 | *
132 | * Write a Java 8 program to sort a given list of strings according to the increasing order of their length.
133 | */
134 | sortByLengthOfList();
135 | /**
136 | * Find the sum and average of all elements in an integer array
137 | *
138 | * Write a Java 8 program to find the sum and average of all elements in an integer array.
139 | */
140 |
141 | calculateAndSumAndAverage();
142 | /**
143 | * Find the common elements between two arrays
144 | *
145 | * Write a Java 8 program to find the common elements between two arrays using streams.
146 | */
147 | commonElements();
148 | /**
149 | * Reverse each word of a string using Java 8 streams
150 | *
151 | * Write a Java 8 program to reverse each word of a given string using the stream API and lambda expressions
152 | */
153 | reverseEachWord();
154 |
155 | /**
156 | * Find the sum of the first 10 natural numbers
157 | *
158 | * Write a Java 8 program to find the sum of the first 10 natural numbers using streams.
159 | */
160 |
161 | int sumOf10NaturalNumber = IntStream.rangeClosed(1, 10)
162 | .sum();
163 | System.out.println(sumOf10NaturalNumber);
164 |
165 | /**
166 | * Reverse an integer array
167 | *
168 | * Write a Java 8 program to reverse an integer array.
169 | */
170 | reversedArray();
171 |
172 | /**
173 | * Find the most repeated element in an array
174 | *
175 | * Write a Java 8 program to find the most repeated element in an array.
176 | */
177 |
178 | mostRepeatedElement();
179 |
180 | /**
181 | * Check if a string is a palindrome using Java 8 streams
182 | *
183 | * Write a Java 8 program to check if a given string is a palindrome using the stream API and lambda expressions.
184 | */
185 |
186 | checkIsTheStringPalindrome();
187 |
188 | /**
189 | * Find strings in a list that start with a number
190 | *
191 | * Given a list of strings, write a Java 8 program to find the strings that start with a number.
192 | */
193 |
194 | stringsStartsWithNumber();
195 |
196 | /**
197 | * Extract duplicate elements from an array
198 | *
199 | * Write a Java 8 program to extract duplicate elements from an array.
200 | */
201 |
202 | extractDuplicateElements();
203 |
204 | /**
205 | * Print duplicate characters in a string
206 | * Write a Java 8 program to print the duplicate characters in a string.
207 | */
208 |
209 | duplicateCharactersInString();
210 |
211 | /**
212 | * Find the first repeated character in a string
213 | * Write a Java 8 program to find the first repeated character in a string.
214 | */
215 | firstRepeatedCharacter();
216 |
217 | /**
218 | * Find the first non-repeated character in a string
219 | *
220 | * Write a Java 8 program to find the first non-repeated character in a string.
221 | */
222 | firstNonRepeatingCharacter();
223 |
224 | /**
225 | * Generate the Fibonacci series
226 | *
227 | * Write a Java 8 program to generate the Fibonacci series.
228 | */
229 | generateFibonacciSeries();
230 |
231 | /**
232 | * Print the first 10 odd numbers
233 | *
234 | * Write a Java 8 program to print the first 10 odd numbers.
235 | */
236 | firstTenOddNumbers();
237 |
238 | /**
239 | * Get the last element of an array
240 | *
241 | * Write a Java 8 program to get the last element of an array.
242 | */
243 |
244 | lastElementInTheArray();
245 |
246 | /**
247 | * Calculate the age of a person in years
248 | *
249 | * Write a Java 8 program to calculate the age of a person in years given their birthday.
250 | */
251 | calculatePersonAgeInYear();
252 | }
253 |
254 | private static void calculatePersonAgeInYear() {
255 | LocalDate birthDate = LocalDate.of(1998, 8, 17);
256 | LocalDate currentDate = LocalDate.now();
257 | int age = Period.between(birthDate, currentDate).getYears();
258 | System.out.println("Age of the person is: " + age);
259 | }
260 |
261 | private static void lastElementInTheArray() {
262 | int[] intArray = {0,1,2,3,4,5};
263 | Integer lastElementInTheArray = Arrays.stream(intArray)
264 | .boxed()
265 | .reduce((first, second) -> second).orElse(-1);
266 | System.out.println("\nlast elements in the array " + lastElementInTheArray);
267 | }
268 |
269 | private static void firstTenOddNumbers() {
270 | Stream.iterate(1,i->i+2)
271 | .limit(10)
272 | .forEach(System.out::print);
273 | }
274 |
275 | private static void generateFibonacciSeries() {
276 |
277 | Stream.iterate(new int[]{0,1},t->new int[]{ t[1], t[0]+ t[1] })
278 | .limit(10)
279 | .map(t->t[0])
280 | .forEach(System.out::print);
281 |
282 | Function> intArraytoListOFInt = array -> Arrays.stream(array).boxed()
283 | .collect(toList());
284 | List collect = Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
285 | .limit(10)
286 | .map(intArraytoListOFInt)
287 | .flatMap(List::stream)
288 | .distinct()
289 | .collect(toList());
290 | System.out.println(collect);
291 | }
292 |
293 | private static void firstNonRepeatingCharacter() {
294 | String tempStr = "rohitrohi";
295 | System.out.println (Arrays.stream (tempStr.split (""))
296 | .filter (str -> tempStr.indexOf (str) == tempStr.lastIndexOf (str))
297 | .findFirst ()
298 | .orElse (""));
299 |
300 | }
301 |
302 | private static void checkIsTheStringPalindrome() {
303 | String str = "momd";
304 | String temp = str.replaceAll("\\s+", "").toLowerCase();
305 | System.out.println("is palindrome string " +IntStream.range(0, temp.length() / 2)
306 | .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)));
307 | }
308 |
309 | private static void firstRepeatedCharacter() {
310 | String word = "rohttoh";
311 | System.out.println (Arrays.stream (word.split (""))
312 | .filter (str -> word.indexOf (str) != word.lastIndexOf (str))
313 | .findFirst ().orElse (""));
314 | }
315 |
316 | private static void duplicateCharactersInString() {
317 | String word = "rohttoh";
318 | System.out.println ("original String " + word);
319 |
320 |
321 | System.out.println (Arrays.stream (word.split (""))
322 | .filter (str -> word.indexOf (str) != word.lastIndexOf (str))
323 | .map (str -> str.charAt (0))
324 | .collect (toList ()));
325 | }
326 |
327 | private static void extractDuplicateElements() {
328 | List duplicateElements = of(1, 2,2,2,3, 3, 4, 5,1,1,56, 7, 8, 9, 10);
329 |
330 | System.out.println("maxed Elements " + duplicateElements);
331 |
332 | List extractDuplicateElements = duplicateElements.stream()
333 | .filter(element -> duplicateElements.indexOf(element)
334 | != duplicateElements.lastIndexOf(element))
335 | .distinct()
336 | .collect(toList());
337 |
338 |
339 | System.out.println("extract duplicates elements from " + extractDuplicateElements);
340 | }
341 |
342 | private static void stringsStartsWithNumber() {
343 | String [] words= {"rohit","foo","nemo","target1","12Target","2robot"};
344 |
345 | System.out.println("original Strings " + Arrays.toString(words));
346 |
347 | List stringStartNumber = Arrays.stream(words)
348 | .filter(word -> Character.isDigit(word.charAt(0)))
349 | .toList();
350 | System.out.println("strings started with a number " + stringStartNumber);
351 | }
352 |
353 | private static void mostRepeatedElement() {
354 | int [] elements = {2,3,1,4,4,1,4,333,3,333,2,2,2,5,222};
355 |
356 | System.out.println("original Array" + Arrays.toString(elements));
357 | Function