Cognizant Java Coding Interview | Nothing is impossible if you solve code like this

preview_player
Показать описание
In this video, we covered Cognizant Java Coding Interview Questions. The candidate got selected in the company with the offer of 9 LPA.

We solved below problem
1.) Place all the occurrence of even number in the array before odd numbers using Java

(paid link)
Recommended Books:
=====================================

(paid link)
Computer & Monitor
=====================================
Рекомендации по теме
Комментарии
Автор

I liked his approach of course pointers is better but this is a simpler method for a junior developer position. Also, as far as 0(n) you might as well make this even simpler imo. Like: public static ArrayList evenThenOdd(int[] array){
ArrayList<Integer> even = new ArrayList<>();
ArrayList<Integer> odd = new ArrayList<>();

for(int i = 0; i < array.length; i++) {
if(array[i] % 2 == 0) {
even.add(array[i] );
} else {
odd.add(array[i]);
}
}

even.addAll(odd);
System.out.println(even);
return even;
}

If you are going the simple route just go really easy. If you want to do 0(1) ok even better but no need to over complicate things.

DriveandThrive
Автор

We can do it in a single pass (iterating all the element only once) & in-place (not creating an extra array, using the input array) by 2-pointers approach.
public static void main(String[] args) {
int[] arr = {1, 2, 5, 4, 7, 8, 11, 20};
int i = 0;
for(int j = 0; j < arr.length; j++){
if(arr[j]%2 == 0){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}
}

}

!! order will change for odd number !!
Hope this is helpful :)

akashm
Автор

Here is another solution which might be useful for some-
We declare another array having double the length of the intial array and then start putting the odd elements on the new array starting from 0th index till initial array's length also simultaenously, we put the even elements in the new array starting from the index, equal to the length of the inital array till needed. Finally we put a condition which checks till when was odd elements and even elements were pushed into the new array and display till those counter values. Code (Hope it's Helpful)-

class oddeven {
public static void main(String[] args) {
int count=0;
int[] arr1={1, 2, 5, 4, 7, 8, 11, 20};
int [] b=new int[(arr1.length)*2];
int evcount=arr1.length;
for(int i=0;i<arr1.length;i++)
{
if(arr1[i]%2!=0)
{
b[count]=arr1[i];
count++;
}

else
{
b[evcount]=arr1[i];
evcount++;
}
}
for(int i=0;i<((arr1.length)*2);i++)
{

{
continue;
}

System.out.print(b[i]+" ");
}
}
}

Thinkdifferentsagnic
Автор

this is too easy i think nowadays they don't ask that kind of questions they must be ask about dynamic programming

Laughing_india_
Автор

There's a company that still asks such easy questions and allows extra space. Good to know!!!

AnuragTripathy
Автор

Really helpful as a fresher, thank you, subbed ✌️

Please keep posting such videos

atulyt
Автор

We can use a queue to store odd elements, nd later copy them in to array without having to run the whole loop once again for odd numbers, nd hence loop runs only for the count of odd numbers . If order should be maintained, else we can use 2 pointer.

reeeeel
Автор

You guys are helping many needy people

k
Автор

Using Stream().filter() we can process the array twice one for even no and other for odd!! Then using string builder concat both the arrays!!

ScientificMyths
Автор

My Solution:
List<Integer> al = new ArrayList<>();
int e = 0;
for (int i : arr) {
al.add(i % 2 == 0 ? e++ : al.size(), i);
}
return al.stream().mapToInt(k -> k).toArray();

RohanKumar-yjsz
Автор

During Gen C(4 LPA), They asked me to reverse an array through swaping, Transpose a Matrix, Basic linear search and Reverse a string.

TiwariAman
Автор

If the order can be compromised then two pointer approach can be used for better space complexity. And that will be optimal solution for this problem.

riturajghosh
Автор

Solved this question using a single loop, although my first approach was also similar to the solution provided in the video.

GeshuLin_WW
Автор

We can also use two pointer method .
Take Start and end variables .
Run a loop which will iterate over array .
Swap both the nos . if start is odd and end is even.
That's it !
( Disadvantage, here is, the order of elements get disturbed )

greyhat
Автор

You provided real scenario . Some youtubers are faking interview questions to get views, by putting much difficult questions

anandkumar-irzj
Автор

Wow man you are lucky... They ask very easy question for 9LPA

Shorts_n_Laughs
Автор

Ur doing O(n square) .. but it is possible to do with O(n) using pointers

josephpraveen
Автор

great bro they are asking very easy problem infosys level problem is much tougher thant this for 9.5lpa role

kapilrana
Автор

another approach could be:

public static void evenThenOdd(int[] arr) {
int i = 0;
int j = arr.length - 1;

while (i < j) {
if (arr[i] % 2 == 0) {
i++;
} else if (arr[j] % 2 != 0) {
j--;
} else {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

for (int k = 0; k < arr.length; k++) {
System.out.println(arr[k]);
}

DriveandThrive
Автор

It is also constructed using for loop in a single method .

falaksajjad