For quite some time I found my time procastinating after getting home from work. Untill a few days ago I stumbled upon the Daily Coding Problem (DCP) and decided to give it a shot.
The code is in C#.
How it works?
The folks at DCP send you a problem that was asked at a top company everyday for you to solve. The premium membership also offers the opportunity to verify your solution.
Problem #1
This problem was recently asked by Google.
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
My solution
using System;
using System.Collections.Generic;
using System.Linq;
namespace Problem01
{
public class Program
{
public static void Main(string[] args)
{
var numbers = Console.ReadLine()
.Split(' ')
.Select(int.Parse)
.ToList();
var k = int.Parse(Console.ReadLine());
var result = TwoNumbersEqual(numbers, k);
Console.WriteLine(result);
}
public static bool TwoNumbersEqual(List<int> numbers, int k)
{
var set = new HashSet<int>();
foreach (var number in numbers)
{
var remaining = k - number;
if (set.Contains(remaining))
{
return true;
}
set.Add(number);
}
return false;
}
}
}
Explanation
The naive solution would be to do two nested for loops and check if any of the combinations gives the k
we desire.
But because I wanted the bonus tag and after some tought, I realized that you could foreach the input array and for each item check find the difference between it and k
. After you find the difference, you can check if you have already seen this number before and the HashSet
data structure allows for constant lookups (because it is a hash table).
I will try to do the daily problem everyday, but sometimes life gets in the way :)
Solutions are posted in my github account
.
Feel free to leave a like and let me know in the comments if I should continue to post my solutions.
If you also have any better solution in mind, by all means share it, so we can learn from each other.
Latest comments (58)
using System;
namespace PlayGround
{
class DailyProblems
{
int[] numArray;
int arraySize = 0;
int sumValue = 0;
}
github.com/subsr97/daily-coding-pr...
This is my git repo which contains solutions in Python! :)
This is a pythonic code:-
Only taking 3 factors for this problem
def num(k,a,b,c):
assert a + b or b + c or c + a == k
if a + b == k:
print("True")
elif b + c == k:
print("True")
elif c + a == k:
print("True")
else:
print("False")
Well as a student here is a less elegant attempt. But I think it gets the job done

Here is my solution in C++
Don't know if it suits the problem given the fact that it'll output true/false on each item in the HashSet but here's my take on the problem
I know Count() maybe adds unneeded complexity and it may not be a one pass.
I'm looking for feedback here.
Rust solution:
Made a sized Set to overcome the reallocation and copying of data. This increases the Size requirement to O(N).
Hi Ivan,
I'm trying to understand this better.
How come you didn't do numbers.contains() instead of set.contains()? Does using the original list of numbers violate one pass?
Because
numbers.contains()
is a list and to see if it contains the element would loop through the whole list (basicallyO(n)
. That makes the solution to have two nested loops and going fullO(n^2)
Probably the shortest
slight improvement with a short circuit (
find
vsfilter
):I just realized both of these solutions fail in the case of
twoNumbersEqual([5], 10)
. This can be fixed by passing the index toincludes
, as such...This should also theoretically be more performant.