Stripe Interview Question: Write a map implementation wi... | Glassdoor

Interview Question

Software Engineer Interview San Francisco, CA

Write a map implementation with a get function that lets

  you retrieve the value of a key at a particular time. t:0 A =1 t:2 A = 2 get(A, t:1) -> 1 get(A, t:3) -> 2
Answer

Interview Answer

5 Answers

2

Used a hashmap where the value was a list of timestamps and values.

Interview Candidate on May 11, 2016
0

Using a hashmap of lists will lead to a O(n) worst case performance (single key with N values), using Map> will get 2O(1) perf, or you could simply have Map

R on Dec 17, 2018
0

public class MyMap
{
    Map> timedMap;

    MyMap()
    {
        timedMap = new HashMap();
    }

    public void putValueInMap(String key, String value, int time)
    {
        timedMap.computeIfPresent(key, (k, v) -> {
            v.put(time, value);
            return v;
        });
        Map valueMap = new HashMap();
        valueMap.put(time, value);
        timedMap.putIfAbsent(key, valueMap);

    }

    public String getValueAtTime(String key, int time)
    {
        Map map = this.timedMap.get(key);
        int index = 0;
        final List collected = map.keySet().stream().sorted().collect(Collectors.toList());
        for (int num : collected)
        {
            if (num <= time)
            {
                index = num;
            }
        }
        return map.get(index);
    }
}

SK on Jun 21, 2019
0

Glassdoor garbled the code formatting completely.
Map is has. String key and Value is another Map whose Key is Integer(timestamp) and Value is String.

SK on Jun 21, 2019
0

Hey guys I used rooftop slushie to get an answer for this interview at Stripe. You should give it a try.

https://wwww.rooftopslushie.com

Anonymous on Jul 10, 2019

Add Answers or Comments

To comment on this, Sign In or Sign Up.