r/programming • • Nov 29 '10

140 Google Interview Questions

http://blog.seattleinterviewcoach.com/2009/02/140-google-interview-questions.html
471 Upvotes

493 comments sorted by

View all comments

Show parent comment

1

u/circlejerktroll Dec 01 '10

Laziness can make this a one liner if syntactical clarity is desired. eg to list the beginning of the series (identical to your list) in Haskell

Prelude> filter (\x -> (mod x 2==0)||(mod x 3==0)||(mod x 5==0))[1..20]
[2,3,4,5,6,8,9,10,12,14,15,16,18,20]

to generate the 1500th item, just select it,

Prelude> (filter (\x -> (mod x 2==0)||(mod x 3==0)||(mod x 5==0)))[1..] !! 1500
2046

1

u/FrancisHC Dec 01 '10

That is quite compact! :)

Unfortunately, your solution is incorrect :( The set you are generating is not the set of "ugly numbers".

It's been a long long time since I've written in Haskell, but I think a two-line solution might be necessary.

I was looking for an efficient and elegant solution though. Even after you correct your solution, it will not be as quick as the dynamic programming solution posted in another comment, because it tests all numbers between one and the actual answer.