Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


Question 201: Find first 3 largest numbers in an



Download 7,31 Mb.
Pdf ko'rish
bet88/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   81   82   83   84   85   86   87   88   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 201: Find first 3 largest numbers in an
array
In this question, the interviewer will most probably ask you to not use
sorting and pick the first/last 3 numbers from the array. Instead he
will ask you to use one “for loop” to solve this problem.
Output:
Three largest elements are: 78, 33, 20


Here, the idea is to have 3 numbers and then iterating through the
array and finding where the current element of array fits in.
At first, we check whether the current element is greater than first, if
true, assign the current element to first number by swapping the
values of first, second and third.
When the first condition is not true, then we compare the current
element with second largest number to find whether the current
number is second largest or not, same goes for third condition.
Question 202: Move all negative numbers at the
beginning of an array and all positive numbers at
the end
Here, also the interviewer will ask not to use any additional data
structure like an extra array and this question can be asked in two
ways, whether the sequence of original array elements should be
maintained or not, so let’s see the program where the sequence is
not maintained:


Output:
-9 -2 -15 -3 1 7 12 5 2
Here, the idea is to iterate through the array and when a negative
number is found, then bring that number to the beginning of the array
by swapping it with the first positive number.
As you can see that the output is not maintaining the original
sequence of array elements.
Now, let’s take a look at the solution which maintains the element
sequence:


Output:
-9 -2 -15 -3 5 1 7 12 2
Now, the output is maintaining the original sequence of array
elements.
There are many programmatic, puzzle problems that the interviewer
can ask. If you are a beginner, then prepare for programs like
Palindrome, Fibonacci, Array problems, String problems, Linked list


programs etc. First, try to solve them with whatever brute-force
solution that comes to your mind, then try to find its time and space
complexity, then try to optimize it. If you are not able to think of a
better solution, no problem, look for the optimal solution on the
internet.



Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   81   82   83   84   85   86   87   88   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish