jeudi 27 avril 2017

Java employment test questions

first time posting here and I'm still fairly junior in Java so please be gentle.

Basically applying for jobs and they get you to do tests to see how proficient you are. The test has since been failed by me, but they never gave the answer for me to learn from. Could you guys please help me to solve this?

Question One

Design and implement a static method map that takes two arguments, a “function” f and a list l, and returns a new list generated from applying f to each element of l. The original list l and its elements should remain unchanged.

Provide example code which uses your map implementation and shows how to implement the function plusOne which takes a number n and returns a new number n+1.

If three is the list containing the numbers 1, 2 and 3, then: map(plusOne, three) will return a new list containing the numbers 2, 3 and 4.

Question Two

You are working on a system which receives prices from an external component at a fast rate. The system needs to process the prices, and typically they cannot be processed as quickly as they arrive, so the implementation must sample the latest price. The external system produces prices for several entities, interleaved into a single sequence of the form:

EntityA: pa1, pa2, pa3…

EntityB: pb1, pb2, pb3…

Time ► ► ►

For example, if during the time taken to process price pa1 the prices pa2, pa3 and pa4 arrive, then the next price the application should process is pa4 and all previous prices should be ignored. Prices for other entities (e.g. pb i) do not affect the latest price for entity a, but are processed independently in the same way with respect to entity b.

Below is the skeleton code for a component that manages prices in this manner. The component needs to be thread-safe (i.e. work without error with concurrent access to update and get prices for the same or different entities, since prices may be updated and accessed by multiple different threads). Complete the code on the following page, implementing the constructor and three methods which are documented.

The following example shows how the component may be used (although single threaded) and the expected output:

Example Output:

    ph.putPrice("a", new BigDecimal(10));

    System.out.println(ph.getPrice("a"));

    ph.putPrice("a", new BigDecimal(12));

    System.out.println(ph.hasPriceChanged("a"));

    ph.putPrice("b", new BigDecimal(2));

    ph.putPrice("a", new BigDecimal(11));

    System.out.println(ph.getPrice("a"));

    System.out.println(ph.getPrice("a"));

    System.out.println(ph.getPrice("b"));

Use the following skeleton: public final class PriceHolder {

       // TODO Write this bit
        public PriceHolder() {
        // TODO Write this bit
        }
        /** Called when a price ‘p’ is received for an entity ‘e’ */
        public void putPrice(String e, BigDecimal p) {
        // TODO Write this bit
        }
        /** Called to get the latest price for entity ‘e’ */
        public BigDecimal getPrice(String e) {
        // TODO Write this bit
        }
        /**
        * Called to determine if the price for entity ‘e’ has
        * changed since the last call to getPrice(e).
        */
        public boolean hasPriceChanged(String e) {
        // TODO Write this bit
        }
    }

Question Three

Extend PriceHolder to provide a method which waits for a price change:

    /**
     * Returns the next price for entity ‘e’. If the price has changed since the last
     * call to getPrice() or waitForNextPrice(), it returns immediately that price.
     * Otherwise it blocks until the next price change for entity ‘e’.
    */
   public BigDecimal waitForNextPrice(String e) throws InterruptedException;

Copy your answer to question two, add this new method and make any necessary modifications to your existing code

Aucun commentaire:

Enregistrer un commentaire