Amazon Interview Question for SDE1s


Team: kindle
Country: United States
Interview Type: In-Person




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

Code from "Java concurrency in Practice"

public class Memoizer<A, V> implements Computable<A, V> {
    private final ConcurrentMap<A, Future<V>> cache
        = new ConcurrentHashMap<A, Future<V>>();
    private final Computable<A, V> c;

    public Memoizer(Computable<A, V> c) { this.c = c; }

    public V compute(final A arg) throws InterruptedException {
        while (true) {
            Future<V> f = cache.get(arg);
            if (f == null) {
                Callable<V> eval = new Callable<V>() {
                    public V call() throws InterruptedException {
                        return c.compute(arg);
                    }
                };
                FutureTask<V> ft = new FutureTask<V>(eval);
                f = cache.putIfAbsent(arg, ft);
                if (f == null) { f = ft; ft.run(); }
            }
            try {
                return f.get();
            } catch (CancellationException e) {
                cache.remove(arg, f);
            } catch (ExecutionException e) {
                throw launderThrowable(e.getCause());
            }
        }
    } }

- gstepanov@griddynamics.com October 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL.

At least match the public interface as asked...

- Anonymous October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Really, what is the point of this code, especially without explanation?

- Anonymous October 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is a hashmap implementation. well, a cache is a sort of hashmap

- Anonymous October 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

where is our dear write back and write through function :)

- odie October 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It looks like hashmap with cache properties, like it should have some ability to remove values when the cache is full implement something like LRU.

- Anonymous January 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually you dont even need to implement nothing for LRU cache, read java doc for LinkedMashMap if you create it with three parametres

new LinkedHashMap(capacity,loadFactor,accessOrder)

Where accessOrder == true, then map will behave like LRU cache with established capacity.

- glebstepanov1992 January 03, 2014 | 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