Linkedin Interview Question for Member Technical Staffs


Team: Tools team
Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

The idea is to use a hashtable or a set for this problem. When the store is called, we store all the numbers into the set. This gives us constant O(1) access time. When we are testing whether the target exists in the set, we simply compute its complement and check whether it exists in the set which amounts to O(n) time complexity. I present my solution in Python below:

Solution:

class StoreNums:
  def __init__(self):
    self.numSet = set()

  # TC:- O(1) since we are using a hashable set
  def store(self, num):
    if num in self.numSet:
      return False
    else:
      self.numSet.add(num)
      return True

  # TC:- O(n) where n = number of nums stored
  def test(self, target):
    for num in self.numSet:
      complement = target - num
      if complement in self.numSet:
        return True
    return False

Test code:

# Test code
s = StoreNums()
print(s.store(1)) # True
print(s.store(3)) # True
print(s.store(5)) # True
print(s.store(3)) # False
print(s.store(6)) # True
print(s.test(4)) # True
print(s.test(5)) # False
print(s.test(6)) # True

- prudent_programmer March 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using a red black tree means you can perform lookups in guaranteed O(log n ) time, and java has a very nice TreeSet data structure which is a RBT with floor(Key key) and contains(Key key) methods already defined on it. Ive included my code below, works as expected with Test inputs and has running time O(log n) for test

import java.util.TreeSet;

public class Solution {
  static private TreeSet<Integer> dataStore = new TreeSet<>();
  
  public void store(int num){
    dataStore.add(num);
  }

  public boolean test(int targetSum) {
	// since it says sum of two numbers we dont even need a loop.
    int l = dataStore.floor(targetSum); // l returns 3
    int diff = targetSum - l; // we subtract and look to see if there is a hit. 
    return dataStore.contains(diff);
  }
}

- syyan4 September 17, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More