Interviewing.io questions, 2023-09-16
parent
5faed85cf1
commit
0a942554b2
|
@ -0,0 +1,49 @@
|
|||
import qualified Data.List as List
|
||||
import qualified Data.Maybe as Maybe
|
||||
import Data.Set (Set)
|
||||
import qualified Data.Set as Set
|
||||
|
||||
{-
|
||||
interviewing.io question:
|
||||
<https://interviewing.io/mocks/linked-in-python-matching-pairs>
|
||||
|
||||
Given a list of whole positive numbers, how would you go about finding two numbers in that list that sum up to a given target number. Assume no duplicates in list.
|
||||
|
||||
-}
|
||||
|
||||
|
||||
wholePositiveNumbers :: Set Integer
|
||||
wholePositiveNumbers = Set.fromAscList [1..1_000_000]
|
||||
|
||||
-- findTargetSumIn xs n =
|
||||
-- (if abs (x-n) `member` xs
|
||||
-- && abs (x-n) + x == n
|
||||
-- then Just (abs (x-n), x)
|
||||
-- else Nothing) : findTargetSumIn xs n
|
||||
-- where
|
||||
-- x =
|
||||
ans lst sum = List.nubBy isMirror $ Maybe.catMaybes $ Set.elems $ Set.map (\x ->
|
||||
let x' = abs (x-sum)
|
||||
in
|
||||
if x' `Set.member` lst
|
||||
&& x' + x == sum
|
||||
then Just (x',x)
|
||||
else Nothing
|
||||
) lst
|
||||
|
||||
isMirror (a,a') (b,b')
|
||||
= a == b'
|
||||
&& b == a'
|
||||
|
||||
example = Set.fromList [14,13,6,7,8,10,1,2]
|
||||
example' = Set.fromList [14,13,6,7,8,10,1]
|
||||
example'' = Set.fromList [1,14,13,3,6,7,2,8,10,4]
|
||||
|
||||
main = do
|
||||
print "Quick 3 answers:"
|
||||
print $ ans example 3
|
||||
print $ ans example' 3
|
||||
print $ ans example'' 5
|
||||
|
||||
print "Long answer"
|
||||
print $ ans wholePositiveNumbers 3
|
Loading…
Reference in New Issue