Sie sind auf Seite 1von 62

1. Data structure and algorithm used in load balancer.

2.
Consider a city with 50 locations each numbered from 0 to 49. Mr. X! runs a ta"i ser#ice
in a city. $e has 25 %a"i&s to ser#ice the 'assengers. (hen 'assenger needs a ta"i he ma)es a
call to Mr. X! and gi#e details li)e his current location as a source* and where he is willing
to tra#el as a destination. $e also tells ma" time he can wait for a ta"i. +n return Mr. X!
either allocate a ta"i to the 'assenger or tell him re,uest can&t be satisfied within the gi#en
ma"-waiting time. .llocated ta"i tra#els from its current location to the 'assengers 'ic)/u'
'oint i.e. the source. %his tra#el is termed as non re#enue tra#el. Mr. X! charge 'assenger
only for the distance from source to the destination. .fter dro''ing 'assenger to the
destination ta"i waits for call from Mr. X! to ser#e ne"t 'assenger.
0et&s assume we )now all %a"i$ire1e,uests in ad#ance. (e also )now the distance and time
to tra#el between any two locations in the city.
(rite a 'rogram which will choose the ta"i&s such that sum of non-re#enue distance
tra#elled by all the %a"i&s is minimum and the number of unsatisfied re,uests are minimi2ed.
.lso 'rint the total non-re#enew distance and number of unsatisfied re,uests.
3i#en 4 dices.5ach dice has . faces.%hat means each dice has numbers from 1 to ..3i#en
6um 6*7ind the number of ways to ma)e the sum 6 if dices are rolled together
8. $ow do you find the greatest 1000 elements in a list of a million elements9 4o other
information gi#en. (hat would be the runtime9 $int: ou can do better than ;<n log n=. +
didn>t reali2e but it could be 'ossible with %ree or $ea's.
4. 7latten a 0ist?0ist?+nteger@@ in Aa#a and im'lement the has4e"t<= and ne"t<= methods.
e.g. BBC*DE*4E should return true when at C* D and false at 4.
5. +m'lementation of .d#anced set which ha#e the functionality as F6etF in cGG along with
e"tra functionality/1andom number generator.1eturns the random number from the set.
C. 3i#en a directed acyclic gra'h.$ow to re'resent it in the relational database for efficient
retrie#al of all the children nodes and all the 'arents of any node.<e" a/@b here b is child of a
and a is 'arent of b=
H. 3i#en a string.7ind the longest substring in it such that the substring contains only 2 uni,ue
characters.5"/ aabbcbbbadef .ns/bbcbbb
D. +n a 'arty there are n different/fla#ored ca)es of #olume I1* I2* I8 ... In each. 4eed to
di#ide them into J 'eo'le 'resent in the 'arty such that
/ 5ach member of 'arty gets e,ual #olume of ca)e <say I* which is the solution we are
loo)ing for=
/ . gi#en member should get a ca)e of single fla#our only i.e. ou cannot distribute 'arts of
different fla#ored ca)es to same member.
/ Minimum #olume of ca)e gets wasted after distribution so that means a ma"imum
distribution 'olicy
9. (rite a C 'rogram to search for a gi#en 'attern from #arious files in a directory without
using gre' or any other inbuilt command
10. Design and im'lement a class to generate random numbers in an arbitrary 'robability
distribution gi#en by an array of integer weights* i.e. for intBE w return a number* n* from 0
to w.length / 1 with 'robability wBnE K sum<w=. Lsing an e"isting random number generator
with a uniform distribution is 'ermitted.

2

5"am'le distribution:
w M 1* 2* 8* 2* 1
5"am'le 'robabilities:
w K sum M 1K9* 2K9* 1K8* 2K9* 1K9
5"am'le results:
n M 0* 1* 2* 8* 4
Documentation:
Class Na#a.util.1andom
'ublic int ne"t+nt<int n=
1eturns a 'seudorandom* uniformly distributed int #alue between 0 <inclusi#e= and the
s'ecified #alue <e"clusi#e=* drawn from this random number generator>s se,uence. %he
general contract of ne"t+nt is that one int #alue in the s'ecified range is 'seudorandomly
generated and returned. .ll n 'ossible int #alues are 'roduced with <a''ro"imately= e,ual
'robability.
Oarameters:
n / the bound on the random number to be returned. Must be 'ositi#e.
1eturns:
the ne"t 'seudorandom* uniformly distributed int #alue between 0 <inclusi#e= and n
<e"clusi#e= from this random number generator>s se,uence
%hrows:
+llegal.rgument5"ce'tion / if n is not 'ositi#e
11. 7ind the most fre,uently occurring element in a P6%. +n this P6% we can ha#e
leftnode?Mrootnode?Mrightnode.
12. (rite a function which returns )th element from the tail in a lin)ed list.
18. +m'lement +terator class with 'ee)<= functionality in Aa#a.
14. ou ha#e ) lists of sorted integers. 7ind the smallest range that includes at least one number
from each of the ) lists.
7or e"am'le*
0ist 1: B4* 10* 15* 24* 2CE
0ist 2: B0* 9* 12* 20E
0ist 8: B5* 1D* 22* 80E
%he smallest range here would be B20* 24E as it contains 24 from list 1* 20 from list 2* and 22
from list 8.
15. (hat is the difference between a com'uters hea' and it>s stac)9
1C. Plac)list all the nodes in a P/tree* when #iewed from all 4 directions.
1H. 6ort a singly/lin)ed list of un)nown si2e using constant s'ace.
1D. ou are trying to to daemoni2e an un)nown* blac)/bo" binary e"ecutable. %he binary
e"ecutable returns no out'ut to 6%D;L% or 6%D511. .ssume that the mystery binary
return code is non/2ero. (hat troubleshooting ste's might you ta)e to learn more about
what the binary is su''osed to do* and why it is failing9
19. During boot* after the P+;6 'erforms a successful 'ower/on/self/test* describe e#erything
that occurs until the console is 'resented to the user.
20. $ow do you ma)e sure an .O+ does not lea) memory9
21. ou are gi#en a dictionary* in the form of a file that contains one word 'er line. 5.g.*
abacus
deltoid
gaff
giraffe
micro'hone
reef
,ar
ou are also gi#en a collection of letters. 5.g.* Qa* e* f* f* g* i* r* ,R.
%he tas) is to find the longest word in the dictionary that can be s'elled with the collection
of
letters. 7or e"am'le* the correct answer for the e"am'le #alues abo#e is SgiraffeT. <4ote that
SreefT is not a 'ossible answer* because the set of letters contains only one SeT.=
22. +n'ut is a number of words. Construct a listing of #alid C/letter words. ou ha#e access to
bool +sIalid<const stringU word=V +m'lement +nsert<= and 3et<= for this listing.
28. 5sca'e strings. Con#ert s'ecial .6C++ characters to form of WXooo&* where Wooo& is oct digit
of the corres'onding s'ecial character.
%he ascii characters that smaller than s'ace are regarded as s'ecial characters.
24. 1eturn a shortest 'refi" of ?code@word?Kcode@ that is ?em@not?Kem@ a 'refi" of any word
in the ?code@list?Kcode@
e.g.
word: cat* it has 4 'refi"es: ST* ScT* ScaT* ScatT
list: al'ha* beta* cotton* delta* camera
1esult is ScatT
25. 6u''ose we use binary search tree to im'lement set* design an algorithm that we can get an
random element from the set* while maintain all the other set o'erations ha#e same
com'le"ities.
2C. %here is an island which is re'resented by s,uare matri" 4"4.
. 'erson on the island is standing at any gi#en co/ordinates <"*y=. $e can mo#e in any
direction one ste' right* left* u'* down on the island. +f he ste's outside the island* he dies.
0et island is re'resented as <0*0= to <4/1*4/1= <i.e 4"4 matri"= U 'erson is standing at
gi#en co/ordinates <"*y=. $e is allowed to mo#e n ste's on the island <along the matri"=.
(hat is the 'robability that he is ali#e after he wal)s n ste's on the island9
(rite a 'suedocode U then full code for function
F float 'robabilityofali#e<int "*int y* int n= F.
2H. 3i#en the 5nglish al'habet* >a> through >2> <lowercase=* and an imaginary onscreen )eyboard
with the letters laid out in C rows and 5 columns:
a b c d e
f g h i j
k l m n o
p q r s t
u v w x y
z
Lsing a remote control / <u' / >u>* down >d>* left >l>* right >r> and enter >Y>=* write a function that
gi#en a word will 'roduce the se,uence of )ey 'resses re,uired to ty'e out the word on the
onscreen )eyboard. %he function should return the se,uence string.
4;%5: %his was an actual ,uestion. + didn>t 'ull this from %o'Coder.
2D. gi#en sorted intBE .* intBE P. $ow would you find the maiden that would ha#e been if both
were combined to one big sorted array9 Lse di#ide and con,ure recurssion.
'lease write method F3etMutualMaidenF and e"'laint it>s time and s'ace com'le"ity
29. 6ort the in'ut character array based on the dictionary gi#en.
7or eg:* +f in'ut word is S6$55OS* sorting will ma)e it as S55$O6S.
Put in the dictionary* 5 may not be at first. .s 'er the dictionary* if O is first* 6 followed and
5 later and finally $.
%hen sorted array is SO655$S.
80. how would you im'lement a secondary sorting. Meaning sorting by Category .* and then
sub sorting by category P9
81. 3i#en an array of integers such that each element is either G1 or /1 to its 'receding element.
7ind 1st occurrence of a gi#en number in that array without using linear search.
82.
7or 2 gi#en array aBE and PBE* find the highest inde" of . such that logical array .B0...iE and
.B4/1...4/1/iE are same.
3i#en an array of integers such that each element is either G1 or /1 to its 'receding element.
7ind 1st occurrence of a gi#en number in that array without using linear search.
7unction to re#erse a c style sub/string
start / 'oints to the first character to be re#ersed
end / 'oints to character after the last character to be re#ersed
4ote: 6%0 not allowed
#oid re#erse<charZ start* charZ end=
1. 4/Oetrol bun) 'roblem: %here are n 'etrol bun)s located in a circle. (e ha#e a truc)
which runs 1 )m 'er 1 liter <mileage 1)m'l=. %wo arrays are gi#en. %he distances between
'etrol bun)s are gi#en in one array. ;ther array contains the no of liters a#ailable at each
'etrol bun). (e ha#e to find the starting 'oint such that if we start at that 'oint * you we
would able to #isit entire circle without running out of fuel. +nitially truc) has no fuel
write a code to 'rint the second largest element in a list
6hortest 'ossible com'le"ity.
%here is a $ealthMonitor and two 6er#ers <Orimary and 6econdary=* all connected to one
and another.
%he $ealthMonitor )ee's 'inging both the ser#ers at s'ecific time inter#als and waits for
their res'onse for a time/out 'eriod after the re,uest has been sent.
%he ser#er res'onds with a health status of itself and of its neighbor <meaning Orimary
res'onsds: ;JV 45+3$P;1-;J=
+m'lement the ser#er>s code to send and recei#e res'onses and then ta)e action based on
res'onse.
(rite a class that will ha#e following functions:
long Chec);ut<=
Chec)+n<long=
1ange of #alues is 1 to 0;43-M.X
.t any gi#en 'oint in time chec)out should return the minimum a#ailable 0;43 number
Chec)in can return the #alue bac)
4o need to chec) for border conditions <e.g. chec) out when all #alues are e"hausted=
+m'lement:
1. long chec)out<=
2. #oid chec)+n<long in'ut=
(rite a class 7or Contacts on a de#ice
+m'lementing 6earch a contact was the biggest 'roblem + faced <because search should
'otentially search: 7irst4ame* 0ast4ame* .ddress* O$[* 5mail etc=
(rite a class for a 'ar)ing garage:
;ne le#el
;ne entry 'oint
4o membershi' or 'ayments re,uired
$andles multi'le ty'es of cars
+m'lement:
1. a search that will return all the strings that match a sub/string
2. an insert into this datastructure
Class
{
Insert (string str{!"
#ist$strings% &redictions(string sub'tring{!"
!
+m'lement an iterator for a Pinary tree. +t should ha#e the following things:
1. bool $as4e"t<=
2. ?%@ 4e"t<=
+t should be an in/order tra#ersal.
+f a function is der mostCommonChar<6tring str* int num= *
1/.7irst in'ut is .abra Ja Daabra and second argument is 1 then the function should return
first most re'eated character in the string .Means in sorted descending .
2/@ 7irst in'ut is .abra Ja Daabra and second argument is 2
then the function should return second most re'eated character in the string
li)e wise 8rd 4rth ....etc
(rite a C 'rogram to find the number of shift re,uired to con#ert one string to another.
Chec) all the corner cases.
5g: abc to acb oK' shd be 2 as >b> shifted from 1st inde" to 2nd and >c> shifted to 1st from
second.
.dding Iery 0arge 4umbers. (rite clean code for it. 'lease chec) all corner cases..
4umber can be really really large
.nswers
Design a singleton design 'attern.
Design a class in CGG such that only one obNect of it can be created.
/ 4ascent on May 02* 2018 in +ndia 5dit \ 1e'ort Du'licate \ 7lag
.ma2on 6oftware 5ngineer K De#elo'er

7ind the 8rd closest element in a bst.ou will be gi#en a 'ointer to root and a #alue within
the tree against which the closest has to be figured out. <closeness is in terms of #alue* not by
distance = and then follow u' ,n: for finding the )th closest in a bst.

. #ideo streaming ser#er is generating the following data. 7ind the 'otential customers
facing buffering issues.
. 'erson is said to face buffering issues when he hits the 'lay button multi'le times on the
same #ideo
ou are gi#en a huge file <say 13P= that contains the following data:
Customer+d/%ime6tam'/5#ent/Iideo+d/Iideolength
0040 /01.00'm /Olay /Iideo1 /02:80:00
(rite code for this. (hat data structure will you use
$e also said* lets say all the 'arsing is ta)en care of and you are gi#en a collection of classes
that contain the abo#e data:
Class
{
CustomerId
(ime'tamp
)vent
*ideoId
!
Design an online hotel reser#ation system.
<+ thin) this has been 'osted in Careercu' earlier=
Design a furniture store with %ables and chairs. (rite a constructor for chair and table

ou are gi#en a L4+X 'ath with dot <current= and two dots <'arent=. Con#ert this to an
absolute 'ath
5.g. ]KhomeKabcK..K.KdefK.KghiK..K.
becomes ]KhomeKghiK
+n'ut is gi#en a binarytree and out is sum of the all the children data and its node data .
5g:
1 2D
2 8 11 1C
4 5 C H 4 5 C H
/ 'haniteNa.24 on .'ril 2H* 2018 in +ndia 5dit \ 1e'ort Du'licate \ 7lag
.ma2on 6D51

/1
of 1 #ote
20
.nswers
3i#en a sorted array consisting 0>s and 1>s. find the inde" of first >1>. write a com'lete
'rogram which ta)es less time com'le"ity. and test all boundary conditions also.
5g: +f gi#en array is 0*0*0*0*0*0*1*1*1*1 the out 'ut should be C.
5"'lain Collaborati#e Caching9
%here are lots of string in a file. 7ind the longest string that could be made from the other
strings in the file.
5g.
the
there
after
thereafter
reaf
3i#en an array . of length n where each element is 1..) where ) is much smaller than n*
find one set of distinct indices i1 and i2 such that .Bi1E G .Bi2E MM 2.
Code to create a file system.... $a#e classes li)e directory* file and all
'lease write the full code
+n a P6%* + want to re'lace all nodes with #alue which is the sum of all the nodes which are
greater than e,ual to the current node.
5
2 10
;ut'ut //@
15
1H 10
ou are gi#en a P6%* and min* ma" elements. our tas) is to trim this P6% so that it
contains the elements between the min and the ma" elements.
7or e"am'le* gi#en the mi" and ma" elements B5* 18E and the tree below* you would return
the out'ut below.
+
, -.
- / -0
0 1 -,
out'ut should be :///@
+
/ -.
1 -,
Design a D6 for storing browsing history.
+ntersection of two lists of unsorted integers.
$ow would you im'lement an infinite counter9
(rite a long running 'rogram in C. %his 'rogram should not hog the COL* use no
slee'<=Kbloc)<=Kselect<=Kwait<=* should not bloc) resources...and should terminate after a #ery #ery
#ery long time
3i#en a #ery #ery #ery large integer<with 1 million digits* say=* how would you con#ert this integer
to an .6C++ string
(hat is the Minimum .mount not 'ossible using an infinite su''ly of coins <Lnbounded )na'sac)=
ou are gi#en coins of
Denominations Q#1* #2 * #8* #4 ....#n R of weight Qw1* w2* w8 .....wnR
4ow that you ha#e a bag of ca'acity ( .
7ind the smallest Ialue I that is not 'ossible to ha#e in the bag.
<4ote * you are not trying to ma"imi2e the #alue I=
7li' a monochrome bitma' around its centre/line* where in'ut is: char Zbytes* int width* int height .
5"am'le:
+n'ut
0101 0110
1111 0011
;ut'ut
0110 1010
1100 1111
%hree co)e machines. 5ach one has two #alues min U ma"* which means if you get co)e from this
machine it will load you a random #olume in the range Bmin* ma"E. 3i#en a cu' si2e n and
minimum soda #olume m* show if it>s 'ossible to ma)e it from these machines.
3i#en a P6% and a #alue ". 7ind two nodes in the tree whose sum is e,ual ". .dditional s'ace:
;<height of the tree=. +t is not allowed to modify the tree
ou are gi#en a list of a million <"* y= ma' co/ordinates of restaurants on a %a'e dis). ou are also
gi#en what your co/ordinate is <a* b= .
ou are re,uired to gi#e out the nearest restaurant in terms of distance. ou are free to assume that
the distance is measured on a straight line basis <bee/line=.
ou cannot hold all the million data simultaneously* and design an efficient data structure to
com'lete this tas).
(rite the following function
#oid draw0ine<byteBE img* int len* int wid* int r* int "1* int "2=
such that you draw a line from "1 to "2 at row r.
len is the length and wid is the width of the imageKcan#as.
6etting a 'i"el on to draw the line is to set the corres'onding bit on the img array
5ach byte corres'onds to D 'i"els* that is each 'i"el is a bit in the array
+magine you ha#e a se,uence of the form 012845CHD9101112181415... where each digit is in a
'osition* for e"am'le the digit in the 'osition 5 is 5* in the 'osition 18 is 1* in the 'osition 19 is 4*
etc.
(rite a function that gi#en a 'osition returns the digit in that 'osition.
<ou could thin) that this se,uence is an array where each cell only holds one digit so gi#en an
inde" return what digit is in that inde"* howe#er you cannot really create an array since the
se,uence is infinite* you need a way to based on the inde" calculate the digit that goes there=.
%he function has to return a single digit.
;ther e"am'les:
inde" M 100* result M 5
inde" M 80* result M 2
inde" M 81* result M 0
inde" M 1000* result M 8
how would you store and find the to' 10 ,ueries in google from some day <when you begin to
count= till a certain date9
gi#e e"am'les of hash functions that ma's english word to byte
+m'lement in ;;O the old cell/'hones game Fsna)eF. +m'lement the function FMo#eL'F. 7ollow
u': how would you reduce s'ace com'le"ity9
Oots of gold game: %wo 'layers . U P. %here are 'ots of gold arranged in a line* each containing
some gold coins <the 'layers can see how many coins are there in each gold 'ot / 'erfect
information=. %hey get alternating turns in which the 'layer can 'ic) a 'ot from one of the ends of
the line. %he winner is the 'layer which has a higher number of coins at the end. %he obNecti#e is to
Fma"imi2eF the number of coins collected by .* assuming P also 'lays o'timally. . starts the game.
%he idea is to find an o'timal strategy that ma)es . win )nowing that P is 'laying o'timally as
well. $ow would you do that9
.t the end + was as)ed to code this strategyY
Design a web browser li)e Chrome with the tab functionality.
.lso describe the data structure to be used.
gi#en a ma2e <matri" boolB4EBME= where 0 M free way* 1M obsticle / $ow many ways are there to
reach from B0EB0E to B4EBME9 write a non/recursi#e solution.
(rite a function that gets a billion integers. $ow can you find the midian in most efficient way
<time=9
same ,uestion* but the in'ut is an endless stream of integers* and we want to find the current
median.
+m'lement a set that su''orts insert* remo#e and get1andom5lement<= o'erations.
e"'lain and write algorithm that im'lements and infinite binary counter* where add<= ta)es ;<1=
time com'le"ity
write a function to sum two single lin)ed lists that re'resents binaries numbers
3i#en a string and a dictionary. Prea) the string into meaningful words.
enumurate all 'hone numbers by the digit dial on the 'hone )ey'ad
longest common subse,unce: gi#en two lists* find the longest sublist <in order= that is the same
longest increasing subse,uence: gi#en a list of numbers* find the largest which is sorted
write the most efficient <in terms of time com'le"ity= function get4umber;fOrimes which ta)es in
an integer 4 as its 'arameter.
to return the number of 'rime numbers that are less than 4
6am'le %estcases:
+n'ut [00:
100
;ut'ut [00:
25
+n'ut [01:
1000000
;ut'ut [01:
HD49D
%he diameter of a tree is the number of nodes on the longest 'ath between two lea#es in the tree.
%he diagram below shows a tree with diameter nine* the lea#es that form the ends of a longest 'ath
are shaded <note that there is more than one 'ath in each tree of length nine* but no 'ath longer than
nine nodes=.
+n 'articular* note that the diameter of a tree % is the largest of the following ,uantities:
the diameter of %>s left subtree
the diameter of %>s right subtree
the longest 'ath between lea#es that goes through the root of %
3i#en the root node of the tree* return the diameter of the tree
(rite a method that generates a random se,uence of numbers of s'ecific 'ercentages
3i#en an array . of 4 integers* we draw 4 discs in a 2D 'lane such that the +/th disc is centered on
<0*+= and has a radius of .B+E. (e say that the A/th disc and J/th disc intersect if A ^ J and A/th and
J/th discs ha#e at least one common 'oint.
(rite a function:
int number-of-disc-intersections<int .BE* int 4=V
that* gi#en an array . describing 4 discs as e"'lained abo#e* returns the number of 'airs of
intersecting discs. 7or e"am'le* gi#en 4MC and:
.B0E M 1 .B1E M 5 .B2E M 2
.B8E M 1 .B4E M 4 .B5E M 0
intersecting discs a''ear in ele#en 'airs of elements:
0 and 1*
0 and 2*
0 and 4*
1 and 2*
1 and 8*
1 and 4*
1 and 5*
2 and 8*
2 and 4*
8 and 4*
4 and 5.
so the function should return 11.
%he function should return _1 if the number of intersecting 'airs e"ceeds 10*000*000.
.ssume that:
4 is an integer within the range B0..10*000*000EV
each element of array . is an integer within the range B0..214H4D8C4HE.
Com'le"ity:
e"'ected worst/case time com'le"ity is ;<4Zlog<4==V
e"'ected worst/case s'ace com'le"ity is ;<4=* beyond in'ut storage <not counting the storage
re,uired for in'ut arguments=.
5lements of in'ut arrays can be modified.
3enerate a solution when multi'le threads want to Nust read in their critical sections and when
nobody is writing in the critical section.
7inding border of a binary tree.3i#en a Pinary tree 'rint all the nodes that form the boundary.
7or gi#en 4Z 4 matri"*
1 2 8
D 9 4
H C 5
(rite a 'rogram to
'rint 1*2*8*4*5*C*H*D*9
how do you handle your thread* to a#oid dead loc) and efficient<generally ,uestion=
(rite a 'rogram to swa' )th node from first and )th node from last in a lin)ed list .
3i#en a matri" that contains 0s and 1s* find the shortest e"it and 'rint the 'ath. ou can na#igate in
to'*bottom*left or right directions.
+n a stream of numbers* )ee' trac) of 1 million ma" numbers.
find the 0angford se,uence for a gi#en 4 if it e"ists9 Details of 0angford se,uence /
htt's:KKen.wi)i'edia.orgKwi)iK0angford-'airing
KK`. 3i#en an array of integers*write a function that retrie#es uni,ue instances of any du'licates*
returning them in a KKnew array /
KK B2*1*2*4*8*1*5*1E
KKM B2*1E
KK B1*1*1*1*1*1*1*1*1E
KK MB1E
KK (rite test cases for this function
(a' to find )th largest element in a binary search tree
+n a sho'* 'roduct X is a#ailable in different ,uantities ,1*,2*,8...... with 'rice tags '1*'2*'8*...
wa' to 'urchase X of ,uantity ` such that total 'rice is less and also number of baggage is less
ZConsider the cost to be o'timum than baggage.
<6orry the trouble guys* + ha#e edited the ,ues here=
KK`. 3i#en an array of integers*write a function that retrie#es uni,ue instances of any du'licates*
returning them in a KKnew array /
KK B2*1*2*4*8*1*5*1E
KKM B2*1E
KK B1*1*1*1*1*1*1*1*1E
KK MB1E
KK (rite test cases for this function
$ow does a site li)e 7aceboo) store F0i)esF 9
(hats the best a''roach for 6'ace com'le"ity and %ime com'le"ity 9 Can we do it in ;<1= s'ace or
at least ;<n= s'ace 9
3i#en a 'olygon with 4 #erte"es and 4 edges. %here is an int number<could be negati#e= on e#ery
#erte" and an o'eration in set<Z*G= on e#ery edge. 5#ery time* we remo#e an edge 5 from the
'olygon* merge the two #erte"es lin)ed by the edge<I1*I2= to a new #erte" with #alue: I1 o'<5=
I2. %he last case would be two #erte"es with two edges* the result is the bigger one.
1eturn the ma" result #alue can be gotten from a gi#en 'olygon.
(rite a method that ta)es a camilCase string as a 'arameter and returns underscore-case as out'ut.
.ssume that in'ut can be null or em'ty. +f CamilCase 'arametar starts with a ca'ital letter turn it
into lower case without 'uting underscore before it. $ow do you test this method9
synchroni2ation algorithm
gi#en:
unsigned char read-byte<=V a side effect that it ad#ances a byte 'ointer in the stream
write:
unsigned char read-sync-byte<=V a may result in @1 calls to read-byte<=
remo#e byte >08> from the stream if the stream is in 'attern 00 00 08
5"am'le:
read-byte<=:
00 0f 42 1H 00 00 08 H4 00 00 00 00 14 ...
read-sync-byte<=:
00 0f 42 1H 00 00 H4 00 00 00 00 14 ...
. binary tree is gi#en with left and right 'o'ulated but the ne"t1ight 'ointers in each node are not
'o'ulated. Oo'ulate the ne"t1ight 'ointer in each node.
ou are gi#en a s,uared integer matri" of n"n si2e in which all rows and all columns
are sorted in ascending order. %he tas) is to chec) if the gi#en matri" contains a gi#en )ey.
3i#e a min and ma" of an integer array* write a function to randomly return a number inside of this
range* but not in the list. .lso write a class that contains this function.
(e define C<n= as the number of ways to ta)e n identical obNects out of a buc)et* where obNects may
be ta)en 1* 2* or 8 at a time.
5"am'le: C<4=MH* because you can ta)e 4 obNects in the following H ways:
1*1*1*1
2*1*1
1*2*1
1*1*2
2*2
8*1
1*8
(rite a function for C<n= in the language of your choice.
$ow would you define a data structure that stores a set of #alues <i.e.* a #alue cannot a''ear more
than one time=* and im'lements the following functions:
add<'=//adds the #alue ' to the set
delete<'=//remo#es the #alue ' from the set
getrandom<=//returns a random #alue from the set <all items should be e,ually li)ely=. <.ssume you
ha#e access to some nice random<= function.=
+m'lement a buc)et fill function for a bitma' image. .ssume your bitma' image is a 2/d array of
integers* where each integer corres'onds to a different color. buc)etfill should ta)e three in'uts*
newcolor* "* and y* and change the color to newcolor in the largest contiguous monochromatic
region containing the 'oint <"*y=
Puild a )ey/#alue data structure which can 'erform following 2 functions
/ loo)u'
/ range0oo)u'<)ey1* )ey2=
3i#en two string 61 and 62. 61 contains from ./! and 62 contains ./!* Z and 9
(here Z means any character 0 or any number of times
(here 9 means any character 0 or 1 number of times
(rite a 'rogram to determine whether 62 matches 61
c M Wa&
w M Sa''leT
c co#ers w* iff w contains c.
c-set M QWa&* Wb&* Wc&* Wg&R
w-set M QWa''le&* Wibm&* Wcisco&* Wgoogle&R
c-set co#ers w-set* iff e#ery w in w-set is co#ered by some c in c-set.
3i#en c-set and w-set* (hether w-set is co#ered by c-set9
if w-set is fi"ed say a boo)* how to determine whether a c-set co#ers this w-set9
/ 2yfo2 on December 2C* 2012 in Lnited 6tates 5dit \ 1e'ort Du'licate \ 7lag
3oogle 6oftware 5ngineer K De#elo'er Data 6tructures
0of 0 #otes14
(hy here out'ut is not Deri#ed Class9999
[include ?iostream@
using names'ace stdV
class Pase Q
'ublic:
charZ nameV
#oid dis'lay<= Q
cout ?? name ?? endlV
R
RV
class Deri#ed: 'ublic Pase Q
'ublic:
charZ nameV
#oid dis'lay<= Q
cout ?? name ?? F* F ?? Pase::name ?? endlV
R
RV
int main<= Q
Deri#ed dV
d.name M FDeri#ed ClassFV
d.Pase::name M FPase ClassFV
Deri#edZ d'tr M UdV
KK standard con#ersion from Deri#edZ to PaseZ
PaseZ b'tr M d'trV
KK call Pase::dis'lay<=
b'tr/@dis'lay<=V
R
KK ;L%OL%
Pase Class
/ (e ha#e table:
create table em'loyee <id int* name #archar* salary decimal* mgr-id int=V
(e want to 'rint name and salary of em'loyees that ma)e more than their manager or don>t ha#e a
manager.
%his can be done by the following 6`0:
select name* salary
from em'loyee e* em'loyee m
where e.salary @ m.salary and e.mgr-id M m.idV
`uestion: how to do this with $adoo'9 (rite down the function for the ma''er and reducer
res'ecti#ely.
ma': )*# /@ <)&*#&=:
reduce: )* ?list #@* /@)&* #&:
$ow can you efficiently im'lement three stac)s in a single array* such that no stac) o#erflows until
there is no s'ace left in the entire array s'ace.
+m'lement memmo#e .
Dont use e"tra memory.
$ow to o'timise it further9
+m'lement a method <in Na#a= that ta)es an +terator?%@ iterator and an int sam'le6i2e and returns a
random sam'le no larger than sam'le6i2e from the iterator.
L ha#e a number* don>t )now how long it is* do not )now how many digits* don>t )now when
number ends* do not )now which is the last number. %here is a function to increment the number by
1* but function can ta)e only stream of digits and not the com'lete number e.g if you ha#e DHD999
as a number* you could in'ut this number into the function only as single digit e.g D*H*D*9*9*9. %he
out'ut should be the whole number incremented by 1 i.e DH9000* remember only single digits you
can send to function as in'ut. ou can use any data structure* but need to tell why you are using that
'articular data structure. 4o need to worry about %ime com'le"ity.
Jindly* suggest how to a''roach this 'roblem 9
3i#en a seria of 'oints <Xi* i=* find the line containing the highest number of 'oints from the list.
Oer my ,uestion he mentioned that + can assume that there is a gi#en function that recei#es two
'oints and returns the a and b of the line eu,ation <aXGb=
im'lement has4e"t<= and ne"t<= methods li)e the Aa#a collection iterator>s* but has4e"t<= chec)s if
collection has negati#e #alue only* and ne"t<= only return negati#e <assume collection has intergers=
we will name a number Faggregated numberF if this number has the following attribute:
Nust li)e the 7ibonacci numbers
1*1*2*8*5*D*18.....
the digits in the number can di#ided into se#eral 'arts* and the later 'art is the sum of the former
'arts.
li)e 11285D* because 1G1M2* 1G2M8* 2G8M5* 8G5MD
12248C* because 12G24M8C
1299111210* because 12G99M111* 99G111M210
112112224* because 112G112M224
so can you 'ro#ide a function to chec) whether this number is aggregated number or not9
3i#en 0ist of some ser#ers* if a ser#er has some 'roblem and if we want to load balance the ser#ers
how would you 'erform add* delete or 'ic) a ser#er at random most efficiently.
Matri" of si2e M"4 filled with characters is gi#en. .ssume that we ha#e a dictionary of words and
searching whether a word 'resent can be done efficiently. 7ind all dictionary words. . word can
tra#erse in any direction* but the 'osition should not be re'eated for a 'articular word.
5": . word can occu'y <1*1=*<2*1=*<2*2= 'ositions in matri".
(ords can o#erla'<5"1: understand//@ under* stand * 5"2: mismatch*mista)e*match*ta)e=
3i#en a source string and a destination string write a 'rogram to dis'lay se,uence of strings to
tra#el from source to destination. 1ules for tra#ersing:
1. ou can only change one character at a time
2. .ny resulting word has to be a #alid word from dictionary
5"am'le: 3i#en source word C.% and destination word D;3 * one of the #alid se,uence would be
C.% /@ C;% /@ D;% /@ D;3
.nother #alid se,uence can be
C.% /@ C;% / @ C;3 /@ D;3
;ne character can change at one time and e#ery resulting word has be a #alid word from dictionary
Define a structure K class to hold a #ery big number <way bigger than bigint= and add a member
functions to increment the number by 1 and decrement the number by 1
Class .* P and C all cout their own names in their constructor. C is P&s subclass while it has an
instance of . as its 'ri#ate member. (hen a C obNect is instantiated* what will the order of 'rinted
letters9
CGG class animal has #irtual method render which 'rints S.nimalT while its subclass %iger has
#irtual method render which 'rints S%igerT. %iger )ittyVV .nimal Z beast M U)ittyV what is the
outcome of beast/@render<=9
+f a )ey has to be inserted in binary tree* say the #alue of root as well as the )ey to be inserted as
same. (ill the )ey becomes left child or right child of 1oot9 Can binary tree ha#e du'licate #alues9
+f yes* why* +f no why9
7rom the set of natural integer numbers
0et " M 1284 M Q1* 2* 8* 4R
0et y M 2410 M Q2* 4* 1* 0R
(rite an algorithm to com'ute the rearrangement of " that is closest to y but still greater than y.
Poth " and y ha#e the same number of digits.
6o in the e"am'le abo#e* the answer would be Q 2* 4* 1* 8 R M 2418 which is greater than y M 2410
and closer than any other arrangements of ".
.nd whats the time com'le"ity of this algorithm9
6tring get6entence<6tring te"t* 6et?6tring@ dictionary=V
KK te"t is a string without s'aces* you need to insert s'aces into te"t* so each word se'erated by the
s'ace in the resulting string e"ists in the dictionary* return the resulting string
KK running time has to be at least as good as ;<n=
KK get6entence<FiamastudentfromwaterlooF* QFfrom* FwaterlooF* FhiF* FamF* FyesF* FiF* FaF*
FstudentFR= /@ Fi am a student from waterlooF
3i#en a array of integers * find 8 inde"es i*N*) such that* i?N?) and aBiE ? aBNE ? aB)E. Pest 'ossible is
a ;<n= algorithm.
(rite a function to determine node in a tree at ma"imum de'th * with ties to the right < ties to the
right means * right most node at Ma"De'th D =
ou are gi#en an array of n elements B1*2*....nE. 7or e"am'le Q8*2*1*C*H*4*5R.
4ow we create a signature of this array by com'aring e#ery consecuti#e 'ir of elements. +f they
increase* write + else write D. 7or e"am'le for the abo#e array* the signature would be FDD++D+F.
%he signature thus has a length of 4/1. 4ow the ,uestion is gi#en a signature* com'ute the
le"icogra'hically smallest 'ermutation of B1*2*....nE. (rite the below function in language of your
choice.
#ectorZ 7indOermute<const stringU signature=V
(rite a 'rogram which ma)es use of two classes named integer and character. %he class integer has
one integer data
member lying in the 'ri#ate section and one member function named in'ut lying in the 'rotected
section.%he member
function in'ut is used to enter the data in the data member.%he class character 'ublically inherits
from the class
integer.%he class character has one character data member and two member functions named calc
and dis'lay.%he calc
function as) the user to in'ut data in the character data member which will be a number from 1/
9.%he calc function will
di#ide the integer number <Data member of the class integer= entered earlier with the number
entered in the character
and the dis'lay function will dis'lay the message SDi#ides Com'letelyT in the case the integer is
di#ided com'letet and
the message SDoes not di#ideT otherwise.%he .6C++ #alues of numbers 0/9 range from 4D/5H.5g.
run is as follows:
5nter +nteger:10
5nter Character :2
1esult: Di#ides com'letely.
(rite a 'rogram which contains a class named 6tring <'lease ta)e care of the case=. %he class
should be ca'able of
holding a string containing more than one word. %he class should at least ha#e a 'arametric
constructors by which we
are going to su''ly a sentence at run time. Class string should be defined in this way that the
following statement
should be e"ecuted with their 'ro'er out'ut.
6tring str1V KKstring with length 0
6tring str2V
cin@@str2V KK to 'rint the char elements of str2V
<Don&t use any string library function for the any 'ur'ose=
ou are free to add any CGG conce'ts* any other data members* member functions and constructors
you thin) are
necessary to sol#e the 'roblem.
(rite a 'rogram which contains a class named 6tring <'lease ta)e care of the case=. %he class
should be ca'able of
holding a string containing more than one word. %he class should at least ha#e a 'arametric
constructor by which we
are going to su''ly a sentence at run time. %he class should contain a member function
W'alindromechec)<=&which
re#erses the words of the sentence in their 'ro'er original order. %he class 6tring should be user
defined. ou are free
to add any other data members* member functions and constructors you thin) are necessary to sol#e
the 'roblem.
6am'le 1un:/
5nter sentence:/
Mombnothing can be better than you
;ut'ut:/
6entence with re#ersed words is:/
you than better be can nothing bmoM
4ot Oalindrome
6am'le 1un:/
5nter sentence:/
wife lo#es husband*husband lo#es wife
;ut'ut:/
6entence with re#ersed words is:/
wife lo#es husband*husband lo#es wife.
Oalindrome
ou are free to add any CGG conce'ts* any other data members* member functions and constructors
you thin) are
necessary to sol#e the 'roblem.
Design a web counter to gi#e how many hits 'er second* 'er minute and 'er hour <i.e.* what )ind of
data structure and algorithm would you use to do this9=.
3i#en a matri" re'resented as intBnEBnE* rotate it 90 degrees cloc)wise in/'lace. <+n/'lace means
minimal e"tra memory to be used* ie* don>t ma)e a new array to co'y into=. 1otate cloc)wise means
to'/row becomes right/column* right column becomes bottom/row etc.
$ow do you sort a billion rows of data of integers <a few gigabytes= in a file with only 1024JP of
main memory.
com'are Fwrite through cacheF and Floo) aside cacheF
assume that dictionary has only 5 words...
.OO05*.O5*P.P*P.00*C.%
write a 'rogram which will acce't a string and list all 'ossible words in the dictionary which start
with that string.use binary trees for fast retrei#al in Na#a
im'lement adding two unsigned numbers without using FGF or FGGF
3i#en a matri" with 1>s and 0>s* find the number of grou's of 1>s. . grou' is defined by
hori2K#ertically adNacent 1>s.
(rite a 'rogram to determine whether nK2 distintinct#e 'airs can be formed from gi#en n integers
where n is e#en and each 'air>s sum is di#isible by gi#en ). 4umbers cannot be re'eated in the
'airs* that means only you can form total nK2 'airs.
3i#en 2 arrays .*P* where "<..length= ? y<P.length=* we want to
insert <y / "= 0>s to . at #arious 'laces such that .ZP is minimum. 7or instance* if . M <1* /1= and
P M <1*2* 8* 4=* then inserting two 0>s in the middle of . such that . M <1* 0* 0* /1= would minimi2e
.ZP. + thin) he was loo)ing for a dynamic 'roblem solution.
(rite a 'rogram to count the number of uni/#alue sub/trees in a gi#en tree. 5"'lain reasoning* and
im'lementation decisions.
Design a system similar to how tinyurl.com website wor)s. 6calability* reliability etc. 'arameters
should be stressed u'on.
%he ma"imum suffi" of a string is the le"icogra'hically largest suffi" of the string. %he ma"imum
suffi" 'roblem is to find the ma"imum suffi" of a gi#en string. 0inear time algorithm re,uired.
ou are gi#en n dices each with heads numbered from 1 to m. ou are to throw the n dices and note
down the sum of the numbers on each of the n dices. ou>ll be gi#e a number " and its a win if the
sum obtained is greater than ". %he 'roblem is the find out the winning 'robability gi#en n* m and
".
4ote 1?Mn?M100*
1?Mm?M10*
m?M"?MnZm.
3enerate a number is range <1*n= but not in a list <i*N=
for e"am'le range is <1*1000=* list is B2*8*5*9*199*200*844E
3i#en a number re'resented as an array of digits* 'lus one to the number.
ie. 1000 is B1*0*0*0E result is B1*0*0*1E
$ow to chec) num is 'ower of 29
3i#en a P6%* ma"imum and minimum #alue* find the sum of nodes with #alues between the abo#e
range
(rite the code to find le"icogra'hic minimum in a circular array* e.g. for the array
PC.PD.D.P* the le"icogra'hic mininum is .PPC.PD.D
(rite an algorithm to find the Wne"t& node <e.g.* in/order successor= of a gi#en node in a binary
search tree where each node has a lin) to its 'arent.
3i#en a nZn grid ma' that is 'arallel with "* y a"is* the down left 'oint is <0*0=*the u' right 'oint is
<n*n=. 3i#en n rectangles* the down left 'oint and u' right 'oint are <"1*y1=<"1>*y1>=*...* <"n*yn=
<"n>*yn>=. "n*yn*"n>*yn>*n are non/negati#e integers* and all rectangles are 'arallel with a"is. Design a
,uery which return* for a s'ecific grid unit <"*y=<"G1*yG1=* how many rectangles co#er it9 Minimi2e
the time com'le"ity of the ,uery and the 're/'rocessing. 1?n?1000
3i#en tree integers a* b*c. (rite a function: int median <int a*int b*int c= to get the median number
among a*b*c. Can not use sort* the times of integer o'erations <e.g. com'are* G / Z K* bit com'uting=
the less the better. .naly2e the best and the worst situation.
3i#en a file of 4 bytes. 7ind a sub/string of minimal length that is not 'resent in the file.
3i#en an 4 " M matri" ha#ing only 'ositi#e #alues* we ha#e to nullify the matri" i.e ma)e all
entries 0.
(e are gi#en two o'erations
1= multi'ly each element of any one column at a time by 2.
2= 6ubtract 1 from all elements of any one row at a time
7ind the minimum number of o'erations re,uired to nullify the matri".
4ote: no range of in'ut was gi#en
(hat could cause a test case run on the same 'iece of code to 'ass sometimes and fail sometimes9
su''ose a structure :
struct item
char nameB15EV
int 'riceV
long +DV
char ty'eBDEV
V
if there is a ser#er running on little endian C4bit machine and a client running on big endian 82bit
machine how will client send the structure to ser#er so that the 'roblem of 'ortability* endianness*
and alignment does not arise9
client :
sendto<sd*<#oid Z=Umyitem* si2eof<item=* 0* <struct soc)addr Z= User#er-addr* si2eof<ser#er-addr==V
ser#er:
rec#from<=V
+f you>re gi#en a list of countries and its corres'onding 'o'ulation* write a function that will return a
random country but the higher the 'o'ulation of the country* the more li)ely it is to be 'ic)ed at
random.
(rite an algorithm to insert a new #alue into a circular sorted lin)ed list.
+nter#iew `uestion c how to im'ro#e a 'arallel cache* this is the most difficult ,uestion i got. + had
no idea what the inter#iewer meant and he clearly had some >right> answer in his mind* but no clue.
$ow would you store 1 million 'hone numbers9
Count the number of set bits for an 82 bits integer. $ow to im'ro#e the 'rocess for the second time
use if the memory is unlimited9
;n a 2/D grid* the 'ositions <"*y= of 8 'ersons are gi#en. 7ind the meeting 'oint such that sum of
distances of each 'erson from meeting 'oint is minimi2ed.
4ow generali2e this to 4 'ersons and sol#e.
3i#en an array of intergers. (rite a 'rogram to 'rint all the 'ermutations of the numbers in the
array. %he out'ut should be sorted in a non/increasing order. 7or e"am'le for the array Q 12* 4* CC*
D* 9R* the out'ut should be:
9DCC412
9DCC124
9D4CC12
....
....
124CCD9
%hree strings say .*P*C are gi#en to you. Chec) weather 8rd string is interlea#ed from string . and
P.
5": .MFabcdF PMF"y2F CMFa"ybc2dF. answer is yes. o<n=
+ ha#e 1 million ,ueries on dis) to sort* my local memoryKcatch can store u' to 1 thousand ,ueries*
how can + 'erform sort9
3i#en a line* adNust this line to the 'age width.
7or e"am'le* gi#en FDog is cuteF <length of chars is 11= and the 'age width is 15* adNust the line to
FDog is cuteF. %he e"tra s'aces should be distributed as much e#en as 'ossible. .ssume there is no
s'ace before the first word or after the last word.
how will you test if the random number generator is generating actual random numbers
if an array is gi#en li)e 11
increment its #alue to 1
oX' should be 12
if the array has 99
oX' should be 100
you can use #ectors.
gi#en in'ut
iX' : B1*2E B2*8E B8*4E
iX': 2*8
oX' should be: B1*2E B2*4E
gi#en an array of integers generate out 'ut using 'roduct of 4/1.
eg: if in'ut is 1*2*8*4
out'ut will be each 'roduct of other int in the array : eg: 2Z8Z4 * 1Z8Z4 *1Z2Z4 * 1Z2Z8
sol#e in 4 not in 42 algo.
gi#en an array of sorted integers with du'licate #alues * sort the array so that there are only uni,ue
#alues left in sorted order < do not use any additional data ty'e * do in'lace sort =
3i#en a list of strings U string M2DJ* where M2DJ re'resents a string which starts from M * ends
with J and has 2Dchars in between . 7ind if M2DJ is uni,ue in the list of strings or not9
FMin<Ma" elements rows= not less than Ma"<Min of columns=F
can you tell me whether abo#e statement ist true or false..
if it true* then tell me solutoin.
if it false* then tell me e"am'le
+m'lement rotate function for forward/only iterators.
Clarification: with ;<1= additional memory. 1otate semantics should be that of std::rotate.
3i#en 'reorder tra#ersal array of a P6%* recontruct the P6%.
3i#en .O+:
int 1ead409C<charZ buf=V
+t reads data from a file and records the 'osition so that the ne"t time when it is called it read the
ne"t 4) chars <or the rest of the file* whiche#er is smaller= from the file.
%he return is the number of chars read.
Lse abo#e .O+ to +m'lement .O+
Fint 1ead<charZ buf* int n=F which reads any number of chars from the file.
3i#en a series of numbers as the in'ut* the last one as the result. Lse the rest numbers to calculate
the result*only G* /* Z* K are allowed. %he order of the in'ut cannot be changed. +f there is an
e,uation* 'rint itV or 'rint Fno e,uationF. +f more than one solution* any wor)ing e,uation is fine.
e"am'le:
in'ut: 2* 8* 1* 4
out'ut: 2G8/1 M 4.
+m'lement the 'lus'lus o'erator when we are getting the in'ut as integer array M Q 9*9*9*9 R.out'ut
will be Q1*0*0*0*0R
(rite a data structure to ca'ture long dataty'e9 %hat means this data structure will )ee' more them
1000 or million digits.
3i#en two strings .Orint all the interlea#ings of the two strings.
+nterlea#ing means that the if P comes after . .+t should also come after . in the interlea#ed string.
e"/
.P and CD
.PCD
.CPD
.CDP
C.PD
C.DP
CD.P
3i#e an e"am'le of times you&#e used thread safe code.
(rite a function that recei#es three integer in'uts for the lengths of the sides of a triangle and
returns one of four #alues to determine the triangle ty'e <1Mscalene* 2Misosceles* 8Me,uilateral*
4Merror=. 3enerate test cases for the function assuming another de#elo'er coded the function
+m'lement a circular ,ueue of integers of user/s'ecified si2e using a sim'le array. Oro#ide routines
to initiali2e<=* en,ueue<= and de,ueue<= the ,ueue. Ma)e it thread safe.
Design an algorithm that* gi#en a list of n elements in an array* finds all the elements that a''ear
more than nK8 times in the list. %he algorithm should run in linear time < n @M0 =
ou are e"'ected to use com'arisons and achie#e linear time. 4o hashingKe"cessi#e s'aceK and
don>t use standard linear time deterministic selection algo
3i#en two P6% 'rint the element in sorted form
com'le"ity ;<n= time ma"m alloted s'ace will be ;<height of bigger tree=
eg V
%1
8
1 5
%2
4
2 C
oK' 1 2 8 4 5 C
+m'lement a bi/directional ma' in Aa#a. 3i#en a )ey* ma' should return corres'onding #alue and
gi#en a #alue* it should return the )ey.
3i#en an array ha#ing 'ositi#e integers* find a continous subarray which adds to a gi#en number.
. log of wood has n mar)s on it. Cost of cutting wood at a 'articular mar) is 'ro'ortional to the
length of the log. %he log of wood can be cut at all the mar)s. 7ind the o'timal order of the mar)s
where the log should be cut in order to minimi2e the total cost of cutting.
Orint .ll common ancestor of 3i#en Pinary tree in ;<n=
without using 5"tra s'ace.
3i#en a dictionary of strings B strings are in sorted orderE you ha#e to find the 'recedence of
characters according to the dictionary..
eat
b"y
e is ran)ed abo#e b according to the dictionary.
(hat is a 'rocess.
(hat is a stac).
$ow many stac)s can a 'rocess ha#e.
(hat is a thread.
$ow many threads can a 'rocess ha#e.
Do the threads ha#e their own stac)s* or share the 'rocess stac)<s=* or both9
3i#e you an array of 6tring*
return number of distinct strings in that array.
1andomly return a node in a binary tree* 'rogram in CKCGG* and define the class or struct of the
binary tree by yourself.
+nterest 'roblems:
when you de'osit a mount of money "1 in a one year * then you get amount of money "2 in n years
later. the interest rate 1* "1 and "2 is )nown* calculate n. (rite a CKCGG .O+* return the year. %he
rate is com'ound interest rate.
+f gi#en a #ariable that is changing after e#ery 1 second...
Design a cloc) using that a#riable..
+ am not sure if this ,uestion was more related to ;6 or not...
Describe in detail what new o'erator does* elaborate on memory allocators* )ernel free 'age
re,uests and cascading constructor calls in case of deri#ed classes.
(rite a 'rogram to return the longest re'eating
substring in a string. eg. for FabababF* FababF is the longest re'eating substring.
ou are at a 'olitical con#ention with n delegates* each one a member of e"actly one 'olitical
'arty. +t is im'ossible to tell which 'olitical 'arty any delegate belongs toV in 'articular* you will
be summarily eNected from the con#ention if you as). $owe#er* you can determine whether any
two delegates belong to the same 'arty or not by introducing them to each otherdmembers of the
same 'arty always greet each other with smiles and friendly handsha)esV members of different
'arties always greet each other with angry stares and insults.
<a= 6u''ose a maNority <more than half= of the delegates are from the same 'olitical 'arty.
Describe an efficient algorithm that identifies a member <anymember= of the maNority 'arty.
design a ser#er architecture for ser#ing 3oogle ma's images
ou are gi#en a 2D array of characters and a character 'attern. (.O to find if 'attern is 'resent in
2D array. Oattern can be in any way <all D neighbors to be considered= but you can&t use same
character twice while matching. 1eturn 1 if match is found* 0 if not.
eg :
Matri"
Q>.>*>C>*>O>*>1>*>C>R*
Q>X>*>6>*>;>*>O>*>C>R*
Q>I>*>;>*>I>*>4>*>+>R*
Q>(>*>3>*>7>*>M>*>4>R*
Q>`>*>.>*>%>*>+>*>%>R
.nd 'attern is microsoft.
%here is a se,uence Qa1* a2* a8* a4* ..... a4R. . run is the ma"imal strictly increasing or strictly
decreasing continuous 'art of the se,uence. 5g. +f we ha#e a se,uence Q1*2*8*4*H*C*5*2*8*4*1*2R (e
ha#e 5 'ossible runs Q1*2*8*4*HR* QH*C*5*2R* Q2*8*4R* Q4*1R and Q1*2R.
3i#en four numbers 4* M* J* 0. Count the number of 'ossible se,uences of 4 numbers that has
e"actly M runs* each of the number in the se,uence is less than or e,ual to J and difference
between the adNacent numbers is less than e,ual to 0.
Design a data cache.
3i#en two arrays which re'resent numbers. (rite a function to add them.
3i#en a set of non o#erla''ing inter#als
5"am'le 1 :<1*4= <C*10= <14* 19= and another inter#al <18* 1H= merge them as <1*4= <C*10= <18*19=
5"am'le 2: <1*5= <C* 15= <20* 21= <28* 2C= <2H* 80= <85* 40=
4ew inter#al <14* 88=
;ut'ut should be
<1*5= <C* 88= <85* 40=
%his is because the new inter#al o#erla's with <C* 15= <20* 21= <28* 2C= <2H* 80=
(rite an .lgorithm and then code for 4 `ueens 'roblem. +f there is no solution 'rint F4o 6olutionF
or else 'rint the board gi#ing out 'ositions of `ueens on the board
%here is a matri"* m " n. 6e#eral grou's of 'eo'le locate at some certain s'ots. +n the following
e"am'le* there are three grou's and the number 4 indicates there are four 'eo'le in this grou'. 4ow
we want to find a meeting 'oint in the matri" so that the cost of all grou's mo#ing to that 'oint is
the minimum. .s for how to com'ute the cost of mo#ing one grou' to another 'oint* 'lease see the
following e"am'le.
3rou'1: <0* 1=* 4
3rou'2: <1* 8=* 8
3rou'8: <2* 0=* 5
. 4 . .
. . . 8
5 . . .
+f all of these three grou's mo#ing to <1* 1=* the cost is: 4Z<<1/0=G<1/1== G 5Z<<2/1=G<1/0==G8Z<<1/
1=G<8/1==
My idea is :
7irstly* this two dimensional 'roblem can be reduced to two one dimensional 'roblem. +n the one
dimensional 'roblem* + can 'ro#e that the best s'ot must be one of these grou's. +n this way* + can
gi#e a ;<3e2= algorithm.<3 is the number of grou'=.
Lse iterator>s e"am'le for illustration:
Q</100*0*100=*<100*0*100=*<0*1*1=R*<"*y*'o'ulation=
for "* Q</100*100=*<100*100=*<0*1=R* 0 is the best.
for y* Q<0*100=*<0*100=*<1*1=R* 0 is the best.
6o it>s <0* 0=
(rite a Orogram to fill em'ty cell of a Oartially filled M"M Crossword Ou22le to form a #alid
5nglish words. 6o the cells initially will be either em'ty * filled or bloc)ed<cannot be filled=. ou
ha#e access to 5nglish Dictionary.
Count smaller elements on right side
eg : B4*12*5*C*1*84*8*2E
oK' B8*5*8*8*0*2*1*0E
(rite a recursi#e function to con#ert Pinary Code of a number into its e,ui#alent 3ray>s code and
the other way round.
3i#en a set of inter#als* find the inter#al which has the ma"imum number of intersections.
\-\
\-\ \-\
\-\ \-\ \-\
\-\ \-\ \-\ \-\
\-\ \-\ \-\ \-\ \-\
5ach cu' has ca'acity C and once a cu' gets full* it dro's half e"tra amount to left child and half
e"tra amount to right child
for 5g : let> first cu's get 2C amount of li,uid then e"tra amount C<2C/C= will be di#ided e,ually to
left and right child cu' of ne"t le#el
i.e. CK2 to left child and CK2 to right child
(rite a function which ta)es in'ut 'arameter as amount of li,uid 'oured at to' <0= and height of
'articular cu' <h= inde" of that cu' <i= and it should return amount of li,uid absorbed in that cu'.
3i#en 2n 'oints on a circle.find the number of ways to draw n non intersecting chords.
3i#en <i= a non/em'ty binary search tree with double #alues <e.g. 8.5= in each node and <ii= a )ey
#alue J
(rite a method to find the closest #alue to J.
(hat is a 'rocess and what is a thread9 (hat are their differences and similarities9 Can you ha#e
'rocesses in 'rocesses9 $ow do you 're#ent deadloc)s9
(rite function that ta)es two integers m and n res'ecti#ely. %he function should fli' the nth bit of
m* counting from the 06P and return the resulting #alue.X
Py fli'* the inter#iewer meant change 0 to 1 and 1 to 0. 7or instance* if you were 'assed in D and 8.
Con#ert D to binary and you ha#e 1000* now we fli' the 8rd bit and we ha#e 1100* then we con#ert
this bac) to decimal<which would be 12=and return the answer.
3enerate all the 'ossible substrings using the characters of a gi#en string. (rite code. <%he order of
chars do not matter* i.e.* ac ?M@ ca=
iK': abc
oK': Q a*b*c*ab*ac*bc*abcR
7irst greater number in an array. 3i#en a large array of 'ositi#e
integers* for an arbitrary integer .* we want to )now the first integer in
the array which is greater than or e,ual . . ;<logn= solution re,uired
e" B2* 10*5*C*D0E
in'ut : C out'ut : 10
in'ut :20 out'ut : D0
3i#en two numbers m and n* write a method to return the first number r that is
di#isible by both <e.g.* the least common multi'le=
ou ha#e a circular trac) containing fuel 'its at irregular inter#als. %he total amount of fuel
a#ailable from all the 'its together is Nust sufficient to tra#el round the trac) and finish where you
started. 3i#en the the circuit 'erimeter* list of each fuel 'it location and the amount of fuel they
contain* find the o'timal start 'oint on the trac) such that you ne#er run out of fuel and com'lete
circuit.
3i#en a list* 0* of si2e ). 0 contains elements between 1 and n. .lso gi#en a function 14D<= such
that this function returns a number between 1 and +4%-M.X.
4ow generate number between 1 and n* using 14D<=* such that the element should not be there in
the list 0. .ll elements should ha#e a uniform 'robability.
7ill in the blan)s:
// // // $ + J 0 M 4 %
// // // 3 A ; O ` 1 6
3i#en an inorder tra#ersal only for a binary tree <not necessarily a
P6%=* gi#e a 'seudo code to generate all 'ossible binary trees for
this tra#ersal se,uence.
7irstly* how many binary trees can be generated gi#en an in/order
tra#ersal9 + )now that gi#en >n> nodes* number of P%s 'ossible is
<2en=/n. Put if we are gi#en a s'ecific in/order se,uence* can we cut
down on this number9
1e#erse n/.ry %ree and 1eturn 0ist of 0eaf 4odes as ;ut'ut
e"am'le
%ree will be li)e
1
K \ X
2 8 4
K K X
5 C H
K X
D 9
out 'ut will be 1st re#erse the tree e.g. 2 'oints to 1 * 5 'oints to 2 * D 'oinst to 5 and so on for each
node * without modifying the tree and then return list containing leaf node
as ;utOut $ead/@ D/@8/@C/@9/@4L00
Code merge sort
$ow will you test this function

5"'lain ,uic) sort and merge sort in details
(rite a function which ta)es an integer and returns the number of >1> bits in its binary
re'resentation.
$ow will you test this function
$ow would you design and im'lement a large social networ)>s <3G or fb= friend recommendation
system 9
3i#en a #ery long list of L10s* find the first L10 which is uni,ue < occurred e"actly once =.
+ ga#e a ;<n= e"tra s'ace and ;<2n= time solution* but he was e"'ecting ;<n= time* one tra#ersal.
3i#en a set of numbers B1/4E . 7ind the number of subsets such that the sum of numbers in the
subset is a 'rime number.
7or a stream of insertions and deletions* recall that "BNE M [insertions / [deletions of element N.
3i#en such a stream satisfying "BNE @M 0 for all elements N* let
. M Q N : "BNE @ 0 R
Determining whether . is em'ty is easy: Nust chec) if the sum of all "BNE>s e,uals 0 <which is easily
doable in a stream=.
Oroblem: de#ise a small memory streaming algorithm to determine if \.\ M 1.
5"tensions: (hat about higher si2es of .9 (hat if the 'romise is not satisfied and we define . to be
the set of N>s with "BNE not e,ual to 0.
.lgorithm to out'ut for a length m of a number stream* the #alue of the element N a''earing in the
stream for which fre,BNE@mK2 with s'ace com'le"ity ;<1= and time com'le"ity ;<m=. Dont need to
worry about the case when there are no elements with fre, @ mK2.
ou ha#e coins with different denominations and you ha#e limited number of each denominations
<some maybe 2ero=* how will you determine if you can su''ly the gi#en change. DO * are you sure9
thin) again.
ou are gi#en >n> a''ointments. 5ach a''ointment contains startime and
endtime. ou ha#e to retun all conflicting a''ointments efficiently
starttime and endtime can range from a few min to few years.
.ssume we ha#e a sorted list of document-id/s for e#ery )eyword. 3i#en a search string* how to
find the documents that ha#e all )eywords in the search string9 Pasically how to find the
intersection in a set of sorted arrays9 (hat is the com'le"ity9 $ow to im'ro#e if there are only two
arrays and one is much smaller com'ared to the other9 $ow to distribute this on a set of different
machines9
3i#en an array .BE* find <i* N= such that .BiE ? .BNE and <N / i= is ma"imum.
3i#en a large file of integers* how to com'ress the file so that we can also do search efficiently9
3i#en a 'ositi#e integer 4* arrange the integers 1 to 4 such the 'osition of the a#erage of two
numbers <if 'resent= falls outside the two numbers. 7or e"am'le* for aBiE and aBNE* if b M <aBiE G
aBNE=K2* then if aB)E M b* ) ? i or ) @ N.
$int :/ +f the a#erage of two numbers is not an integer then we can assume that they do not ha#e an
a#erage.
+m'lement a 1andom.ccessContainer and its iterator in CGG* li)e std::#ector
ou are gi#en a #ery large array of numbers <'ositi#e integers= in any order. Orint the numbers that
are du'licated:
1. 4o memory constraint
2. 0imited Memory
Design a data structure where the following 8 functions are o'timised:
1. +nsert<n=
2. 3et1andom5lement<=
8. 1emo#e<n=
(rite a class* and im'lement the functions. 3i#e com'le"ity of each of these ..

Design a 'arallel Preadth 7irst 6earch algorithm for a directed weighted gra'h.
Pasically you need to find the minimum cost to reach to a node from the starting node ?gi#en@.
<Aust sa#e the o'timum cost and not the o'timum 'ath=. Calculate and out'ut the o'timum
reachability cost for all the nodes from a gi#en starting 'oint.
+m'lement in C with o'enMO.
1. $ow about using D76 or 6hortest 'ath first instead. (ould these algorithms 'erform better than
P76 with 'arallel im'lementation. esK4o (hy9
3i#en an int array which might contain du'licates* find the largest subset of it which form a
se,uence.
5g. Q1*C*10*4*H*9*5R
then ans is 4*5*C*H
6orting is an ob#ious solution. Can this be done in ;<n= time
3i#en an int array which might contain du'licates* find if it is a se,uence.
5g. Q45*50*4H*4C*49*4DR
is a se,uence 45* 4C*4H*4D*49*50
6orting is an ob#ious solution. Can this be done in ;<n= time and ;<1= s'ace
Oartition an int array in 2 e,ual 'arts such that the a#g of two is e,ual.
Oro#ide algo only* dont write code
$ow can you merge two P6% in'lace so that 'reser#ing the P6% 'ro'erty9
Oro#ide algo only* dont write code
3i#en an array of strings of 0s and 1s. X and are also gi#en. 1eturn the ma"imum number of
elements in a subset of the array elements which will X number of 2eroes and number of 1s when
combined. 7or eg: if arrayBE M QF01F* F10F* F0F* F110FR XM8* M2
.nswer should be 8 since first 8 strings when combined will gi#e the re,uired number of 0s and 1s.
Orint the numbers of form 2ei.5eN in increasing order. 7or eg:
1* 2* 4* 5* D* 10* 1C* 20
ou need to im'lement a #ersioned stac)* i.e. #ersion of stac) will increment after each 'ushK'o'.
.'art from 'ushK'o'* im'lement a method 'rint<n= which would 'rint stac) state corres'onding to
#ersion >n>. 7or e"am'le:
/@ initially stac) is em'ty.
/@ Iersion 1: 11 is 'ushed
/@ Iersion 2: D is 'ushed
/@ #ersion 8: 'o'. only 11 left
/@ Iersion 4: 15 is 'ushed
....
.nd so on.
Orint<n= should 'rint state at #ersion >n>.
$ere 1 should 'rint 11* 2 should 'rint D* 11...
.ll methods should be as efficient as 'ossible.
3i#en a matri" which re'resents 1K0 re'resenting a direct flight between two 'laces in a 2D array*
find the number of ho's re,uired to reach from one 'lace to another 'lace. +n other words* find if it
is bi/ho'* tri/ho' etc.
%wo #ery large numbers are re'resented using arrays. Multi'ly these two numbers. 5.g. %wo
numbers 12 and 18 are re'resented as aMB1*2E adn bMB1*8E. %he e"'ected result is 12Z18M15C i.e.
cMB1*5*CE
`2 6ort a list of obNects using the following com'arison o'erator.
is3reater<a*b=
G1 a@b
/1 a?b
0 %he 're#ious com'arison is annulled. KKit means we bac) to 're# 'osition isn&t it 9
write code efficient sorting
1an) a list without sorting it.
0ist : BC* 8* 9E
1an) : B1* 0* 2E
algo U then code it efficiently
(here do you see the smart'hone industry mo#ing9 (hat needs to be done to ma)e users to go in
that direction9
$ow much would it cost to store all the #ideos in the internet9 <4eed to ma)e good assum'tions
li)e no of #ideo ser#ers* file si2eKty'eKencoding etc=
3i#e two binary search trees* 'rint the numbers of both together in ascending order
(rite a function to find the longest common 'refi" string amongst an array of strings
(rite a 'rogram to count the number of columns gi#en the sam'le hitml li)e below:
?%.P05 P;1D51@
?C.O%+;4@. test table with merged cells?KC.O%+;4@
?%1@?%$ 1;(6O.4M2@?%$ C;06O.4M2@.#erage
?%$ 1;(6O.4M2@other?P1@category?%$@Misc
?%1@?%$@height?%$@weight
?%1@?%$ .0+34M057%@males?%D@1.9?%D@0.008
?%1@?%$ .0+34M057% 1;(6O.4M2@females?%D@1.H?%D@0.002
?K%.P05@
;n a dumb terminal* this would be rendered something li)e:
. test table with merged cells
K//////////////////////////////////////////////////X
\ \ .#erage \ other \ Misc \
\ \///////////////////\ category \////////\
\ \ height \ weight \ \ \
\/////////////////////////////////////////\////////\
\ males \ 1.9 \ 0.008 \ \ \
\/////////////////////////////////////////\////////\
\ females \ 1.H \ 0.002 \ \ \
X//////////////////////////////////////////////////K
3i#en a filename say filter-file.c * 'rogram to search for any 'attern in order
e" flt-fl /. true
flt-el / false
search te"t is in order and characters occur multi'le. any 'ermutation. unwanted characters not
allowed.
Oartition a set of numbers into two such that difference between their sum is minimum* and both
sets ha#e e,ual number of elements.
7or e"am'le: Q1* 4* 9* 1CR is 'artitioned as Q1*1CR and Q4*9R with diff M 1H/18M4.
Does greedy wor) here9 7irst sorting* and then 'ic)ing smallest and largest to fall in set 1* and
'ic)ing 2nd smallest and 2nd largest to fall in set 2.
3i#en an acyclic undirected unweighted connected gra'h* find its re'resentation as a tree with the
least height. Prute force is ;<ne2=. Come u' with an ;<n= solution
Design a ,ueue <7+7;= using only stac)s <0+7;=. %he only su''orted o'erations on the stac) are:
Oush<=* Oo'<= and +s5m'ty<=. %he final ,ueue had to im'lement the o'erations `ueue<= and
De,ueue<=. %he 'rogram had to be efficient and handle e"ce'tions.
+ ha#e two methods of an obNect* and they each access a critical section of code. + want to restrict
access to the section so that in one method* + allow multi'le threads to access the critical section. +n
the other method* + want only one thread to ha#e access. +f a caller calls the second method* it
should loc) out all clients from accessing the critical section in either of the two functions. $ere is
the basic structure of the class:
class Class%hat4eeds7i"ing
Q
'ublic:
KK (ill allow many concurrent threads through* unless there is a
KK call to the other method.
#oid .llowMany<= Q
KK $ere is the critical section that must be 'rotected
...
R
KK (ill loc) out any client* including callers to the other method.
#oid .llow;ne<= Q
KK $ere is the critical section that must be 'rotected
...
R
'ri#ate:
KK .ssume there are members here that need 'rotecting
KK abo#e.
...
RV
+n order to sol#e this 'roblem* you are 'ro#ided with two classes: Mute" and 6ema'hore. %hey
ha#e the standard beha#ior of the conce'ts that share their class names. $ere are the 'ublic
interfaces for each of these classes:
class Mute"
Q
'ublic:
Mute"<=V
#oid .c,uire<=V
#oid 1elease<=V
RV
class 6ema'hore
Q
'ublic:
KK .t it>s creation* one can s'ecify the count
KK of the sema'hore.
6ema'hore<unsigned int count=V
#oid .c,uire<=V
#oid 1elease<=V
RV
7i" the Class%hat4eeds7i"ing im'lementation so that the critical section is 'rotected.
our solution will be graded on fle"ibility and robustness <i.e.* we should be able to re/use your
solution in a generic case and it should be e"ce'tion safe=. ou are allowed to create as many
classesKobNectsKtem'latesKetc that you need. 7eel free to use the 6%0 if necessary. Document your
code as you would for real/world maintainability.
3i#en string in'ut1* in'ut2* remo#e where#er the occurrence of in'ut2 in in'ut1.
e.g:
in'ut1: s)Nthshetheshetesm
in'ut2: she
in'ut1 will become Fs)NththetesmF
3i#e the test cases.
Create a structure that will allow you to search a 'erson by first name or last name <or both=* in
better than ;<4= timeframe.
. function 3et(ord7rom0ist ta)es an integer 'arameter and returns the word at the corres'onding
inde". 4ow you are gi#en a word*say FstringF* how do you o'timally get the inde" of the word
where it occurs. ou don>t )now the data structure used to store the words..ssume that all the words
in listKdatabase are sorted..lso assume that the function returns null or some e"ce'tion if the inde"
which is gi#en as in'ut is out of bound
$ow do you go about finding the to' 10 google searches at the end of a day9 +n case you are storing
the information about them* how and where would you store it9 <li)e dis)s etc=
3i#en a sorted array* out'ut all tri'lets ?a*b*c@ such that a/b M c. 5"'ected time is ;<ne2=. My
a''roach using binary search too) ;<ne2 logn=. (hen you attem't an a''roach* test your code with
this e"am'le and list your out'uts for #erification. %han)s.
/12* /H* /4* 0* 8* 5* 9* 10* 15* 1C
(hen 'eo'le search at 3oogle* they often ty'e two words without s'ace. 7or e"am'le* instead of
ty'ing Fbinary treeF* someone can ty'e FbinarytreeF. 3i#en an in'ut* what is a good way of finding
if it is a combination of two #alid words9
0et>s say you ha#e a 'hrase without any s'aces / eg. FthisisawesomeF. 3i#en a dictionary* how
would you add s'aces in this string9
Orint tri'lets that sum to 0 in an integers array
3i#en 4 'ens and n ca's . 6ort them. you cant com'are 'ens with other 'ens and ca's with other
ca's
let f<n* )= be the [ of ways of choosing ) integers without re'lacement from
n consecuti#e integers so that no two selected are consecuti#e.
a. gi#e a recurrence for f<n* )=
b. efficient im'lementation of f<n* )=
0et us assume that we store integers in strings. 1ight an increment function in C that will increment
a gi#en integer by one.
%here are a row of houses* each house can be 'ainted with three colors red* blue and green. %he cost
of 'ainting each house with a certain color is different. ou ha#e to 'aint all the houses such that no
two adNacent houses ha#e the same color. ou ha#e to 'aint the houses with minimum cost. $ow
would you do it9
4ote: Oainting house/1 with red costs different from 'ainting house/2 with red. %he costs are
different for each house and each color.
3i#en a triangle li)e the following
8
4 C
1 5 0
1. $ow many nodes would you ha#e* for 20 rows9
2. $ow to find the largest sum from the to' of the triangle to the one of the nodes at the bottom. +n
other words* if you consider it as a tree* find the ma" sum of all 'aths from root to the leaf.
3i#e two binary search trees* 'rint the numbers of both together in ascending order.
(hat is the 'robability of a )night staying on a chess board after J ho's <in'ut : starting 'osition
and J=
3i#en two dies* modify one such that you get the numbers 1 to 12 in e,ual
'robability
3i#en 2Zn G 8 'oints in 2d s'ace* with no 8 'oints collinear and no 4 'oints lying on a circle* de#ice
an algorithm that will find a circle that contains n 'oints inside it* n outside it and 8 on it.
3i#en an array of 'ositi#e integers that re'resent the bars in a histogram*
find the rectangle with the largest area under the cur#e and abo#e the a"is.
) distinct integers B0* 4=
6elect a random no B0*4= which is not in this ) distinct list.
5"am'le:
B4* C* 9E
Choose a random no between 0 / 9 which is not 4* or C* or 9.
Ialid out'ut: 2
+n#alid out'ut: C
3i#en an array 1 to 4 * how many 'ermutations of it will be Min /$ea' of of 4Y 'ossible
'ermutations
Consider the 'roblem of neatly 'rinting a 'aragra'h on a 'rinter. %he in'ut
te"t is a se,uence of n words of lengths l1* l2* . . . * ln* measured in
characters. (e want to 'rint this 'aragra'h neatly on a number of lines that
hold a ma"imum of M characters each. ;ur criterion of SneatnessT is as
follows. +f a gi#en line contains words i through N* where i f N * and we
lea#e e"actly one s'ace between words* the number of e"tra s'ace characters
at the end of the line is M _ N Gi _ 6um<l)=<from ) M i to N= * which must
be nonnegati#e so that the words fit on the line. (e wish to minimi2e the
sum* o#er all lines e"ce't the last* of the cubes of the numbers of e"tra
s'ace characters at the ends of lines. 3i#e a dynamic/'rogramming algorithm
to 'rint a 'aragra'h of n words neatly on a 'rinter. .naly2e the running
time and s'ace re,uirements of your algorithm.
+m'lement a stac) that 'o's out the most fre,uently added item. 6tac) su''orts 8 functions / 'ush*
'o'*and to'. 3i#e com'le"ity of each functions in your im'lementation.
3enerate n random numbers in the range 0...n/1 without re'etition <using something li)e drand4D
which returns a fraction in the range B0...1=.
$ow to add a counter to www.google.com to trac) the billionth user.
ou ha#e a C4bit interger counter set to 0. $ow long it will ta)e to o#erflow the counter gi#en that
you are incrementing it at 43h2 s'eed
ou ha#e two classes list and array* how can you ma)e sure that array class will com'ile before list
class. Poth the classes might be in differnet files.
$ow to get +6P4 numbers from the billion of files that are downloaded and how to store them9+
mean reading and storing of +6P4 number.
(rite a 'rogram that 'rints all the sub string that is 'alindrome within a in'ut 6tring. 7or e.g 7or
in'ut 6tring FabbcacbcaF out'ut should be: Bcac* bcacb* cbc* acbca* bbE
0et>s discuss on to'ic Fsolution to string 'roblems using suffi" tree U suffi" arrayF
1eorder 1st string based on 2nd string.
eg: <tractor*car=
out'ut: carrtto or carrott or carrtot. %he order of letters not in common doesn>t matter.
Memory management in O$O
Oass by #alue and 'ass by reference in O$O
0iar* 0iar
.s a newbie on a 'articular internet discussion board* you notice a distinct trend among its #eteran
membersV e#eryone seems to be either unfailingly honest or com'ulsi#ely dece'ti#e. ou decide to
try to identify the members of the two grou's* starting with the assum'tion that e#ery senior
member either ne#er lies or ne#er tells the truth. ou com'ile as much data as 'ossible* as)ing each
'erson for a list of which 'eo'le are liars. 6ince the 'eo'le you are as)ing ha#e been around on the
board for a long time* you may assume that they ha#e 'erfect )nowledge of who is trustworthy and
who is not. 5ach 'erson will res'ond with a list of 'eo'le that they accuse of being liars. 5#eryone
on the board can see that you are a tremendous n00b* so they will grudgingly gi#e you only 'artial
lists of who the liars are. ;f course these lists are not to be ta)en at face #alue because of all the
lying going on.
ou must write a 'rogram to determine* gi#en all the information you>#e collected from the
discussion board members* which members ha#e the same attitude toward telling the truth. +t>s a
'retty 'o'ular discussion board* so your 'rogram will need to be able to 'rocess a large amount of
data ,uic)ly and efficiently.
+n'ut 6'ecifications
our 'rogram must ta)e a single command line argumentV the name of a file. +t must then o'en the
file and read out the in'ut data. %he data begins with the number of #eteran members n followed by
a newline. +t continues with n chun)s of information* each defining the accusations made by a
single member. 5ach chun) is formatted as follows:
?accuser name@ ?m@
followed by m lines each containing the name of one member that the accuser says is a liar. accuser
name and m are se'arated by some number of tabs and s'aces. m will always be in B0* nE. .ll
member names contain only al'habetic characters and are uni,ue and case/sensiti#e.
5"am'le in'ut file:
5
6te'hen 1
%ommaso
%ommaso 1
3alileo
+saac 1
%ommaso
3alileo 1
%ommaso
3eorge 2
+saac
6te'hen
;ut'ut 6'ecifications
our out'ut must consist of two numbers se'arated by a single s'ace and followed by a newline*
'rinted to standard out. %he first number is the si2e of the larger grou' between the liars and the
non/liars. %he second number is the si2e of the smaller grou'. ou are guaranteed that e"actly one
correct solution e"ists for all test data.
5"am'le out'ut:
8 2
%o safeguard against the dreaded 'henomenon of wall 'osting while drun)* fli')art is im'lementing
a feature that detects when 'ost content is too garbled to ha#e been done while sober and informs
the user that they need to ta)e an online breathaly2er test before being allowed to 'ost.
Lnfortunately* there is far too much content for a gi#en set of 'ersons to e#aluate by hand.
7ortunately* you are a 'rogrammer of some re'ute and can write a 'rogram that 'rocesses and
e#aluates wall 'osts. ou reali2e that such a 'rogram would be of great use to society and intend to
resol#e the 'roblem once and for all. %he 'rogram you write must com'ute a score for a body of
te"t* returning this score u'on com'letion.
our 'rogram will be gi#en a list of acce'ted words and run on one wall 'ost at a time. 7or each
word ( in the 'ost* you must find word (> from the list of acce'ted words such that the number of
changes from ( to (> is minimi2ed. +t is 'ossible that ( is already (> and thus the number of
changes necessary is 2ero. . change is defined as re'lacing a single letter with another letter* adding
a letter in any 'osition* or remo#ing a letter from any 'osition. %he total score for the wall 'ost is
the minimum number of changes necessary to ma)e all words in the 'ost acce'table.
+n'ut 6'ecification
our 'rogram must ta)e a single string argument* re'resenting the file name containing the wall
'ost to analy2e. +n addition* your 'rogram must o'en u' and read the acce'ted word list from the
following static 'ath location:
K#arKtm'Ktwl0C.t"t
7or testing 'ur'oses* you may download and e"amine the acce'ted word list here. (hen submitting
your code* you do not need to include this file* as it is already 'resent on the machine.
%he in'ut file consists entirely of lower case letters and s'ace characters. ou are guaranteed that
the in'ut file will start with a lower case letter* and that all words are se'arated by at least one s'ace
character. %he file may or may not end with a new line character.
5"am'le in'ut file:
tihs sententcnes iss nout #arrry goud
ou are guaranteed that your 'rogram will run against well formed in'ut files and that the acce'ted
word list is identical to the one 'ro#ided for testing.
;ut'ut 6'ecification
our 'rogram must 'rint out the minimum number of changes necessary to turn all words in the
in'ut wall 'ost into acce'ted words as defined by the word list file. (ords may not be Noined
together* or se'arated into multi'le words. . change in a word is defined as one of the following:
1. 1e'lacing any single letter with another letter.
2. .dding a single letter in any 'osition.
8. 1emo#ing any single letter.
%his score must be 'rinted out as an integer and followed by a single new line.
5"am'le ;ut'ut <newline after number=:
%he secretary 'roblem is an o'timal sto''ing 'roblem that has been studied e"tensi#ely in the fields
of a''lied 'robability* statistics* and decision theory. +t is also )nown as the marriage 'roblem* the
sultan>s dowry 'roblem* the fussy suitor 'roblem* the googol game* and the best choice 'roblem.
%he solution is sometimes called the 8Hg rule. %he 'roblem can be stated as follows:
1. %here is a single secretarial 'osition to fill.
2. %here are n a''licants for the 'osition* and the #alue of n is )nown.
8. %he a''licants can be ran)ed from best to worst with no ties.
4. %he a''licants are inter#iewed se,uentially in a random order* with each order being e,ually
li)ely.
5. .fter each inter#iew* the a''licant is acce'ted or reNected.
C. %he decision to acce't or reNect an a''licant can be based only on the relati#e ran)s of the
a''licants inter#iewed so far.
H. 1eNected a''licants cannot be recalled.
D. %he obNecti#e is to select the best a''licant. %he 'ayoff is 1 for the best a''licant and 2ero
otherwise.
%erminology: . candidate is an a''licant who* when inter#iewed* is better than all the a''licants
inter#iewed 're#iously. 6)i' is used to mean Finter#iew and reNectF.
Clearly* since the obNecti#e in the 'roblem is to select the single best a''licant* only candidates will
be considered for acce'tance. ;ne reason why the secretary 'roblem has recei#ed so much attention
is that the o'timal 'olicy for the 'roblem <the sto''ing rule= has a sur'rising feature. 6'ecifically*
for large n the o'timal 'olicy is to inter#iew and reNect the first nKe a''licants <where e is the base of
the natural logarithm= and then to acce't the ne"t who is better than these inter#iewed candidates.
.s n gets larger* the 'robability of selecting the best a''licant from the 'ool goes to 1Ke* which is
around 8Hg. (hether one is searching through 100 or 100*000*000 a''licants* the o'timal 'olicy
will select the single best one about 8Hg of the time.
+nsert a node into a circular lin)ed list.
%his C00 is sorted. 6o* if the C00 is li)e 1/@2/@4/@C/@1
you will be as)ed to in'ut a node 8 and it must come between 2 and 4.
(rite a 'rogram for this in CGG or A.I.
3i#en 8 arrays .*P*C find three indices i*N*) such that the #alue of + M ma"<<a/b=*<b/c=*<c/a== is
minimi2ed
(hat is the most efficient way* memory/wise* to store 1 million 'hone numbers9 .''arently this is
an inter#iew ,uestion at 3oogle* although this seems li)e its a bit too easy.
Consider there is an array with du'licates and u r gi#en two numbers as in'ut and u ha#e to return
the minimum distance between the two in the array with minimum com'le"ity.
ou are gi#en a 1D array of integers* such as:
intBE array M B8*4*H*2*2*C*0*9EV
6u''ose you need to treat this array as a 2D table with a gi#en number of rows.
ou want to sum the columns of the table.
;ne #alue of num1ows is 4..in that case the resultant array would loo) li)e
what if num1owsMM49
8 4
H 2
2 C
0 9
d/
12 21
(e ha#e a te"t file with 18 Million entries* all numbers in the range from 1.000.000 to 100.000.000.
5ach line has only one number. %he file is unordered and we ha#e about 1g du'licate entries.
1emo#e the du'licates.
3i#en an array . of 4 integers we draw 4 discs in a 2D 'lane* such that i/th disc has center in <0*i=
and a radius .BiE. (e say that )/th disc and N/th disc intersect* if ])Xnot MN] and )/th and N/th discs
ha#e at least one common 'oint.
(rite a function
class 6olution Q 'ublic int number-of-disc-intersections<intBE .=V R
which gi#en an array . describing 4 discs as e"'lained abo#e* returns the number of 'airs of
intersecting discs. 7or e"am'le* gi#en 4MC and
XbeginQdis'laymathR.B0EM1 .B1EM5 .B2EM2 .B8EM1 .B4EM4 .B5EM0XendQdis'laymathR
there are 11 'airs of intersecting discs:
0th and 1st
0th and 2nd
0th and 4th
1st and 2nd
1st and 8rd
1st and 4th
1st and 5th
2nd and 8rd
2nd and 4th
8rd and 4th
4th and 5th
so the function should return 11.
%he function should return /1 if the number of intersecting 'airs e"ceeds 10*000*000. %he function
may assume that 4 does not e"ceed 10*000*000.
3i#en a binary tree* 'rint each le#el on a single line.
you are gi#en following:
1. .n em'ty tan)
2. Lnlimited source of water.
8. 6ome container of certain measurments and a container of 1 litre is always gi#en.
our Nob is to fill the tan) from source of water using the containers in minimum number of ste's.
ou cant fill the container with a small amount of water than its si2e <filling 'artially is not
allowed=.
7ind the number of ste's and 'rint the solution.
e.g.
%an) 6i2e: D0 litre
Containers: 1*8*5*C*25 litre
6olution:
4
5*25*25*25
%an) 6i2e: H1 litre
Containers: 1*8*5*C*25 litre
6olution:
C
8*C*C*C*25*25
(rite a function that* gi#en a string* returns number of characters* words and lines in that string.
3i#en a J"J array* rotate the array 90 degrees counter/cloc)wise in 'lace.
add 2 lin) lists widout recursion in ;<n= n constant s'ace
3i#en a binary tree* find 2 leaf nodes say X and such that 7<X*= is ma"imum where 7<X*= M
sum of nodes in the 'ath from root to X G sum of nodes in the 'ath from root to / sum of nodes in
the common 'ath from root to first common ancestor of the 4odes X and
+s there any algorithm to find distance between each 'air of 'oint among n 'oints whose time
com'le"ity is better than ;<ne2= i.e. C<n*2=.
ou ha#e a digital cloc) ha#ing 24 hr format of $$:MM:66. ou ha#e to find following
'robabilities:
1/ ;ccurence of a 'articular #alue<let say 05= in 66
2/ .s 1st 'oint we ha#e to find 'robabilities for 'articular #alue in hour and minute fields.
8/ 'robalities for other combinations<'articular $$:66 or MM:66 #alues=
ou ha#e to answer the 'robability in terms of digits occurence...for e"am'le in the second>s field..
at unit 'lace 10 digits<0/9= are 'ossible* and in the tens 'lace C digits are 'ossible <0/5=
+t is not a #ery straight forward 'roblem. Many different scenarios are 'ossible<#alue in the seconds
field will change in e#ery 1 min* on the other hand* the #alue in the hour field will remain same till
C0 min=
Olease don>t answer ur 'robabilty in terms of time<as 1KC0* 1K24ZC0= rather in terms of digits only as
described abo#e.
3i#en a binary matri" of 4 X 4 of integers * you need to return only uni,ue rows of binary arrays
eg:
0 1 0 0 1
1 0 1 1 0
0 1 0 0 1
1 1 1 0 0
ans:
0 1 0 0 1
1 0 1 1 0
1 1 1 0 0
Chec) (hether Ooint 0ie +nside* on or outside the triangle efficiently what will be time com'le"ity
U which data structure algorithm
as you can see google as)ed they wants ,uality code U efficient algorithm
.n easier way than what 1ichie mentioned is to the following:
if a 'oint O is inside triangle .PC* then
.rea O.PG.rea OPC G.rea O.CM.rea .PC
notice that if O is on the edge of .P* PC* or C.* the abo#e hold. Put effecti#ely* one of the area
O.P* OPC* O.C is 0 <so Nust ma)e sure you chec) that=.
if O is outside* the abo#e e,uality does 4;% hold..
Put how you will a''roach for if 'oint lie outside the triangle
. rooted binary tree with )eys in its nodes has the binary search tree
'ro'erty <P6% 'ro'erty= if* for e#ery node* the )eys in its left
subtree are smaller than its own )ey* and the )eys in its right
subtree are larger than its own )ey. +t has the hea' 'ro'erty if* for
e#ery node* the )eys of its children are all smaller than its own )ey.
ou are gi#en a set of n binary tree nodes that each contain an
integer i and an integer N. 4o two i #alues are e,ual and no two N
#alues are e,ual. (e must assemble the nodes into a single binary tree
where the i #alues obey the P6% 'ro'erty and the N #alues obey the
hea' 'ro'erty. +f you 'ay attention only to the second )ey in each
node* the tree loo)s li)e a hea'* and if you 'ay attention only to the
first )ey in each node* it loo)s li)e a binary search tree.Describe a
recursi#e algorithm for assembling such a tree
6uggest a D6 for web ser#er to store history of #isited 'ages. %he ser#er must maintain data for last
n days. +t must show the most #isited 'ages of the current day first and then the most #isited 'ages
of ne"t day and so on.
(rite a function which ta)es as 'arameters one regular e"'ression<only 9 and Z are the s'ecial
characters= and a string and returns whether the string matched the regular e"'ression.
3i#en an integer >)> and an sorted array . <can consist of both G#eK/#e nos=* out'ut 2 integers from
. such that a/bM).
O6:
nlogn solution would be to chec) for the occurence of )/aBiE <using binary search= when you
encounter aBiE. methods li)e hash consume s'ace.
+s an ;<n= solution with ;<1= e"tras'ace 'ossible9
5/ 7ibonacci function im'lement it .
then + did +terati#e way to get better 'erformance * then he as)ed about 7ibonacci recursi#e and
what is the com'le"ity of it 9 it is ;<n2= because of the tree calls and re'eated calls.
4/gi#en an array of int and gi#en a number * find the 2 elements in array that add u' to the gi#en
number.
8/ Different ways to communicate between Orocesses in ;6.
2/ Iirtual function table in CGG * and how they wor) and how the com'iler generate them and how
the obNect )now it is o#erridden and is it shared or each obNect ha#e it>s own table.
1/ Difference between .rray and lin)ed list.
(rite a data structure to count number of connections on web ser#er in last 1 minute.
3i#en two unsorted int arrays* find the )th element in the merged* sorted array.
e"am'le:
intBE aM B8 1 HE
intBE b M B4 9E
): 8
return 4.
/ )rish on Aune 08* 2011 5dit \ 1e'ort Du'licate \ 7lag
3oogle 6oftware 5ngineer K De#elo'er .lgorithm
0of 0 #otes24
.nswers 3i#en a two balanced binary search trees.Merge both the trees so that it will form again a
balanced binary search tree.
<4;%5: 1e,uest for correction: should the in'ut bst s ha#e same number of nodes9 if the in'ut bst s
ha#e une,ual nodes* we cant necessarily build a balanced bst for the result =
6u''ose there are 2 'ersons . and P on 7P . . should be able to #iew the 'ictures of P only if
either . is friend of P or . and P ha#e at least one common friend . %he inter#iewer discussed it for
nearly 80 minutes . %he discussion mainly included following 'oints /
1. $ow are you going to store the list of friends for a gi#en user 9
2. 7ile system #s DP
8. 3i#en list of friends of 2 users * how are you going to find common friends 9
4. +f you are going to store the friends in DP then how will the table loo) li)e 9
5. $ow many ser#ers do you need 9
C. $ow are you going to allocate wor) to ser#ers 9
H. $ow many co'ies of data will you need 9
D. (hat 'roblems will you face if you are maintaining multi'le co'ies of data.
3i#en a credit card number with a certain number of fi"ed digits and a certain number of digit
'laceholders* find the number of solutions such that the entire number yields a remainder of 8 when
di#ided by 18. Lse 'ro'erties of modulus
3i#en that you now ha#e 2 geogra'hically se'arated datacenters and a re,uest comes in for a url
that>s not 'resent in your current datacentre* how will you handle it9
Design a site similar to tinyurl.com
3i#en 8 in'ut strings 61* 62* 68 for e"am'le ta)e s1 M FhardF s2 M Fwor)F* s8 M FsuccessF
assign numeric #alues <0/9= to each of the characters in these strings such that e,uation s1 G s2 M s8
holds true. Constraints are that all occurrence of a letter should be assigned the same digit. 1eturn
/1 if not 'ossible. + told the algo using bac)trac)ing but he re,uired in 'olynomial time for which +
had no idea.
3i#en a 8D matri" of mZnZo dimension and there lies a cubicle in each cell of the matri". .ssume
J cubicle are occu'ied<you )now the coordinates of occu'ied cubicles= and remaining are #acant.
ou ha#e to arrange a meeting. 6o find a cubicle such that the sum of the distances tra#eled by all
the 'ersons should be minimum. . Oerson can>t mo#e diagonally* they can only 'arallel to a"es
1. ou are gi#en the Wdownloads& folder on a com'uter which may contain a number of files that are
du'licates of each other. <these files are the same byte/wise but may ha#e diff names= describe a
method which identifies all du'licates in the least amount of time.
2. 4ow there are ) com'uters each with 1000000 files. ou need to determine efficiently the
du'licates across all these com'uters. 6tate the assum'tions in terms of to'ology* connecting de#ice
etc.
6uggest methods to identify s'am mails
`1. (hat is inheritance
2. Oolymor'hism
8. Diff between Oure Iirtual class and Iirtual Class
4. .d# and dis/ad# of C[ and CGG o#er each other
5. 6e,uence in which constructors are called when a child class obNect is created and why is the
order so.
3i#en a rotated sorted array* find the M+4 of the array.
$e 'ointed out a mista)e in my
int middle M <beginGend=K2 which could o#erflow if the array si2e was +4%-M.X.
.nswer was:
middle M <end/begin=K2 G begin
3i#en a binary tree* 'rint its 'erimeter:
node* left/@most nodes from to' to bottom* leaf nodes from left/@ right* right/@most nodes from
bottom to to'
////////////////////////////1
///////////////////////2////////8
//////////////////4/////5/////C////////H
/////////////D//////9/////10//////11/////12
should 'rint:
1/2/4/D/9/5/10/11/12/H/8
5 because it doesn>t ha#e any children. 10 and 11 are children of C and D U 9 are children of 4.
.'ologies for the messy diagram.
3i#en a Pinary tree and a node* return it>s 'ost/order 'redecessor
6ingle +nitiali2ation :
3lobal #ariable "* initiali2ed to 0.
+m'lement a function that can be called by multi'le threads simultaneously or se,uentially.
%he #alue of " should be set to the current time only once. +f it is already set* the #alue shouldn>t be
u'dated.
Ma)e sure that the function doesn>t become a bottlenec)
3i#en a P% and 2 nodes* find 0owestCommon.ncestor
7ind the Ma" sum subse,uence in array
3i#en >n> coins* 'rint the number of ways to form an amount >.> . %his is a standard denomination
'roblem with one small twist<(e ha#e only one coin of each ty'e*not infinite number*if we choose
one coin for ma)ing change we should not choose it again= . Can somebody gi#e a code with
e"'lanation 9
5"am'le:
.mount:8 coins : 1 2 8
%here are only two ways <1*2=<8= not <1*1*1=<1*2=<8=
(rite a 'rogram to calculate 6um of two singly lin)ed lists.
e.g.
1//@2//@8
D/@9/@10
.
1esult list should be 10//@2//@8
ou are not allowed to ma)e any change in in'ut lists. %hose are read only.
3i#en a P6% find Ceiling #alue of gi#en )ey
D
C 12
2 4 11 14
)ey M D return 11
)ey M 1 return 2
)ey M 1C return 4ull
+teration and 1ecursion both
`:
3i#en a binary tree with nodes that ha#e left* right 'ointers 'ointing to the left and right children
res'oecti#ely. +t also has a neighbor 'ointer that currently Ooints to null.
(rite a function to ma)e it 'oint to its neighbor.
5.g
1
2 8
4 5 C H
1.sibling should 'oint to null
2.sibling should 'oint to 8
8.sibling should 'oint to null
4.sibling should 'oint to 5
5.sibling should 'oint to C
C.sibling should 'oint to H
H.sibling should 'oint to null
+teration and 1ecursion both
4 men/ each can cross a bridge in 1*8* H* and 10 min.
;nly 2 'eo'le can wal) the bridge at a time. $ow many min. minutes would they ta)e to cross the
bridge.
%he 'robability of a bus 'assing through a certain intersection in a time window of 20 min. is 0.9
(hat is the 'robability of the same bus 'assing through the same intersection in 5 min.
gi#e an algorithm to find the height of the n/ary tree9
6u''ose we want to con#ert one string 61 to another string 62 using only 8 ty'es of o'erations:
/+nsert<'os*char= <costs D=
/Delete<'os= <costs C=
/1e'lace<'os*char= <costs D=
7ind the se,uence of ste's to con#ert 61 to 62 such that the cost to con#ert 61 to 62 is minimum.
5g. >calculate> to >late> / the 'ossible o'erations are
Delete<0=
Delete<1=
Delete<2=
Delete<8=
Delete<4=
and the abo#e se,uence of o'erations costs 80.
+ used the following code<using le#enshtein algorithm= to sol#e this. Put + am not getting correct
answer.
tu'lesMBE
o'sMBE
s1M>>
s2M>>
def le#enshtein<a*b=:
global s1*s2
n* m M len<a=* len<b=
if n @ m:
a*b M b*a
n*m M m*n
s1*s2Ma*b
current M range<nG1=
for i in range<0*len<current==:
currentBiEMcurrentBiEZD
tu'les.a''end<current=
for i in range<1*mG1=:
're#ious* current M current* BiZDEGB0EZn
for N in range<1*nG1=:
add* delete M 're#iousBNEGC* currentBN/1EGD
change M 're#iousBN/1E
if aBN/1E YM bBi/1E:
changeMchangeGD
currentBNE M min<add* delete* change=
tu'les.a''end<current=
return currentBnE
'rint le#enshtein<>calculate>*>late>=
for i in range<len<tu'les==:
striM>>
for N in range<len<tu'lesB0E==:
striGMstr<tu'lesBiEBNE=G> >
iMlen<tu'les=/1
NMlen<tu'lesB0E=/1
o'sMBE
while i@0:
while N@0:
minimumMmin<Btu'lesBi/1EBNE*tu'lesBiEBN/1E*tu'lesBi/1EBN/1EE=
if minimumMMtu'lesBi/1EBNE:
iMi/1
if not tu'lesBiEBNEMMtu'lesBi/1EBNE:
o's.a''end<B1*iE=
elif minimumMMtu'lesBiEBN/1E:
NMN/1
if not tu'lesBiEBNEMMtu'lesBiEBN/1E:
o's.a''end<B2*i/1*NE=
else:
o's.a''end<B8*i/1*s1Bi/1EE=
iMi/1
NMN/1
+m'lement a sim'le rege" 'arser which* gi#en a string and a 'attern* returns a boolean indicating
whether the in'ut matches the 'attern. Py sim'le* we mean that the rege" can only contain one
s'ecial character: Z <star=. %he star means what you>d e"'ect* that there will be 2ero or more of any
character in that 'lace in the 'attern. $owe#er* multi'le consecuti#e stars are allowed. 6ome
e"am'les of #alid in'ut <and e"'ected out'ut=:
f<aZb* acb= M@ true
f<abcZ* abbc= M@ false
f<ZZbc* bc= M@ true
Design a class structure for an air'ort terminal* where your 'rimary use case is allocating runway
time to a''roaching aircraft. 7or e"am'le* an instance of a terminal may ha#e only two runways of
different lengths and must schedule these among fi#e aircraft of different ty'es re,uesting
'ermission to land.
(rite a ser#ice or ser#ices to su''ort tic/tac/toe between two 'layers* on an infinite board. 4ormal
rules a''ly <i.e. three in a row to win=* but the 'layers are not limited to a 8X8 board and can choose
to 'lace an X or an ; in any arbitrary* 'ositi#e <i* N= 'osition. 6olution should be as s'ace and time
efficient as 'ossible. our ser#ice is only res'onsible for maintaining and u'dating the state of the
board between two 'layers* gi#en their se,uence of mo#es.
%ruth %able im'lementation: (rite a function which ta)es integer as +n 'ut 'arameter <let>s say n=*
'rint all %rue <%= * 7alse <7= combinations n times. $ere is the e"am'le:
for n M 1
;ut'ut :
%
7
for n M 2
;ut'ut :
% 7
7 %
7or n M 8
;ut'ut :
% % %
% % 7
% 7 %
% 7 7
7 % %
7 % 7
7 7 %
7 7 7
3i#en a se,uence of numbers <or array=.7ind the ma"imum distance between all the same
numbers.0i)e you ha#e 1*2*8*4*1*1*H*4 so ma"<1=M5*ma"<2=M0 ma"<4=M4. etc.
3i#en an array of integers* find all sub/arrays whose elements sum 2ero.
1./1*4*/4 has 8 such arrays 1 to /1* 1 to /4 and 4 to /4
3i#en a mathematical e"'ression* remo#e the redundant brac)ets from the e"'ression.
e.g. in'ut: <a G <bZc== Z <d Z < f Z N= =
out'ut should be: <a G b Z c= Zd Z f Z A
o'erations to su''ort: G* /* K* Z* GG* also ternary o'erators.
(rite a function to 'o'ulate the best <lowest cost= 'ath in a C4"C4 weighted grid from a gi#en start
cell to a destination cell.
Design a tas) scheduler
Design a #ending machine.
array of numbers are gi#en. (.O to find the sum of contiguous subarray within a one/dimensional
array of numbers which has the largest sum.
.fter writing 'rogram to return the largest sum modify it to return the start and end inde" of such a
subarray.
3i#en an array with source code and braces <braces means >Q> or >R> = intermi"ed. (.O to return true
of braces are balanced <im'lies that for each o'ening brace there must be a closing brace and for
each closing brace there must be o'ening brace= and false otherwise.
3i#en a 'attern O and a te"t %* (.O to return all indices from % where O matches.
Oosition of Jnight is gi#en on a chessboard.
1eturn me something <adNacency matri" or list or anything= which shows all
the 'ositions the )night can reach u'to from a gi#en 'osition.
+ must be able to tell* from what is returned* if the 'osition is reachable or not
and if reachable + must be able to trace the 'ath from gi#en 'osition to target 'osition

7or e"am'le if 4 cells are reachable from a cell .* then these 4 cells become children of ..
%hen from a cell* say P* out of these 4 cells* you can reach 2 more cells: C and D. %hen C and D
become children of P.
0i)ewise 'rogram need to return me a D6. + ha#e gi#en a #aluable hint with this follow/u'. + ho'e
this will hel'
3i#en an integer* write a 'rogram that con#erts the gi#en number to a number <in base 10=.
%he gi#en number could be in any base* but the base is un)nown.

Das könnte Ihnen auch gefallen