Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Created October 8, 2021 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamrobbie/87c9cecc15a55034743c937c11dfdda8 to your computer and use it in GitHub Desktop.
Save adamrobbie/87c9cecc15a55034743c937c11dfdda8 to your computer and use it in GitHub Desktop.
Interview question solution
defmodule Test do
@moduledoc """
Documentation for `Test`.
"""
@spec find_stack_pair(any) :: Integer
def find_stack_pair(list) do
list
|> pairwise()
|> IO.inspect(label: "after pairing")
|> calculate()
|> List.last()
end
defp pairwise(list) do
list
|> Enum.chunk_every(2, 1, :discard)
end
defp calculate(list) do
list
|> Enum.map(&Enum.sum/1)
|> Enum.sort()
|> duplicates()
end
defp duplicates(list) do
acc_dupes = fn(x, {elems, dupes}) ->
case Map.has_key?(elems, x) do
true -> {elems, Map.put(dupes, x, nil)}
false -> {Map.put(elems, x, nil), dupes}
end
end
acc_dupes
list |> Enum.reduce({%{}, %{}}, acc_dupes) |> elem(1) |> Map.keys()
end
end
@adamrobbie
Copy link
Author

Get better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment