Question

package com.spartasystems.interview; public class FloorPlan { private final int width; private final int length; public FloorPlan(int...

package com.spartasystems.interview;

public class FloorPlan {

private final int width;
private final int length;

public FloorPlan(int length, int width){
this.width = width;
this.length = length;
}
}

package com.spartasystems.interview;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FloorPlanUtility {


/**
* Given a map of building names and floor plans and a number 'n',
* return a list of 'n' building names that were mapped to the
* FloorPlans with the largest areas
*
* Things to consider:
*
* - The output does not need to be sorted
* - Inputs need to be validated to pass all unit tests (throw InvalidArgumentException)
*
*
* @param buildings a map of building name -> floor plan
* @param n the number of buildings with the largest floor plans to return
* @return a List of building names with the largest floor plans
*/
public static List<String> largestNBuildings(Map<String, FloorPlan> buildings, int n) {

return new ArrayList<>();
}
}

Testclass:

package com.spartasystems.interview;

import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class FloorPlanUtilityTest {

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenInputMapIsNull() {
FloorPlanUtility.largestNBuildings(null, 10);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenInputMapIsEmpty() {
FloorPlanUtility.largestNBuildings(new HashMap<>(), 0);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenInputCountIsNegative() {
Map<String, FloorPlan> buildings = new HashMap<>();
buildings.put("b1", new FloorPlan(1, 1));
buildings.put("b2", new FloorPlan(10, 10));
FloorPlanUtility.largestNBuildings(buildings, -1);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenInputCountIsTooLarge() {
// TODO
}

@Test
public void testLargestBuildings() {
Map<String, FloorPlan> buildings = new HashMap<>();
buildings.put("b1", new FloorPlan(1, 1));
buildings.put("b2", new FloorPlan(10, 10));
buildings.put("b3", new FloorPlan(2, 2));
buildings.put("b4", new FloorPlan(3, 3));
buildings.put("b5", new FloorPlan(11, 11));
buildings.put("b6", new FloorPlan(5, 6));
buildings.put("b7", new FloorPlan(8, 9));
buildings.put("b8", new FloorPlan(3, 2));
buildings.put("b9", new FloorPlan(2, 3));
buildings.put("b10", new FloorPlan(9, 9));

List<String> expected = Collections.unmodifiableList(Arrays.asList("b10", "b7", "b2", "b5"));

List<String> result = FloorPlanUtility.largestNBuildings(Collections.unmodifiableMap(buildings), expected.size());

assertThat(result, Matchers.notNullValue());
assertThat(result.size(), Matchers.equalTo(expected.size()));
assertTrue(result.containsAll(expected));
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

class FloorPlan {

    private final int width;
    private final int length;

    public FloorPlan(int length, int width) {
        this.width = width;
        this.length = length;
    }

    public int getWidth() {
        return width;
    }

    public int getLength() {
        return length;
    }


}

class FloorPlanUtility {

    /**
     * Given a map of building names and floor plans and a number 'n', return a list
     * of 'n' building names that were mapped to the FloorPlans with the largest
     * areas
     *
     * Things to consider:
     *
     * - The output does not need to be sorted - Inputs need to be validated to pass
     * all unit tests (throw InvalidArgumentException)
     *
     *
     * @param buildings a map of building name -> floor plan
     * @param n         the number of buildings with the largest floor plans to
     *                  return
     * @return a List of building names with the largest floor plans
     */
    public static List<String> largestNBuildings(Map<String, FloorPlan> buildings, int n) {
        if(buildings == null || n < 0 || buildings.isEmpty() || n > buildings.size()) {
            throw new IllegalArgumentException();
        }


        class BuildingUtil implements Comparable<BuildingUtil>{
            String name;
            FloorPlan plan;

            public BuildingUtil(String name, FloorPlan plan) {
                this.name = name;
                this.plan = plan;
            }

            @Override
            public int compareTo(BuildingUtil o) {
                int a = plan.getLength() * plan.getWidth();
                int b = o.plan.getLength() * o.plan.getWidth();

                return a-b;
            }
        }

        ArrayList<BuildingUtil> buildingUtils = new ArrayList<>();
        for(String name: buildings.keySet()) {
            buildingUtils.add(new BuildingUtil(name, buildings.get(name)));
        }

        // sort the list area wise
        Collections.sort(buildingUtils);

        ArrayList<String> topNBuilding= new ArrayList<>();
        for(int i=buildingUtils.size()-1; i>=0 && i>=buildingUtils.size()-n; i--) {
            topNBuilding.add(buildingUtils.get(i).name);
        }

        return topNBuilding;
    }
}

public class FloorPlanUtilityTest {

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenInputMapIsNull() {
        FloorPlanUtility.largestNBuildings(null, 10);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenInputMapIsEmpty() {
        FloorPlanUtility.largestNBuildings(new HashMap<>(), 0);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenInputCountIsNegative() {
        Map<String, FloorPlan> buildings = new HashMap<>();
        buildings.put("b1", new FloorPlan(1, 1));
        buildings.put("b2", new FloorPlan(10, 10));
        FloorPlanUtility.largestNBuildings(buildings, -1);
    }

    @Test(expected = IllegalArgumentException.class)
    public void shouldThrowExceptionWhenInputCountIsTooLarge() {
        Map<String, FloorPlan> buildings = new HashMap<>();
        buildings.put("b1", new FloorPlan(1, 1));
        buildings.put("b2", new FloorPlan(10, 10));
        FloorPlanUtility.largestNBuildings(buildings, 3);
    }

    @Test
    public void testLargestBuildings() {
        Map<String, FloorPlan> buildings = new HashMap<>();
        buildings.put("b1", new FloorPlan(1, 1));
        buildings.put("b2", new FloorPlan(10, 10));
        buildings.put("b3", new FloorPlan(2, 2));
        buildings.put("b4", new FloorPlan(3, 3));
        buildings.put("b5", new FloorPlan(11, 11));
        buildings.put("b6", new FloorPlan(5, 6));
        buildings.put("b7", new FloorPlan(8, 9));
        buildings.put("b8", new FloorPlan(3, 2));
        buildings.put("b9", new FloorPlan(2, 3));
        buildings.put("b10", new FloorPlan(9, 9));

        List<String> expected = Collections.unmodifiableList(Arrays.asList("b10", "b7", "b2", "b5"));

        List<String> result = FloorPlanUtility.largestNBuildings(Collections.unmodifiableMap(buildings),
                expected.size());
        System.out.println(result + " ");
    }
}

Please upvote, as i have given the exact answer as asked in question. Still in case of any concerns in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
package com.spartasystems.interview; public class FloorPlan { private final int width; private final int length; public FloorPlan(int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • package rectangle; public class Rectangle {    private int height;    private int width;    public...

    package rectangle; public class Rectangle {    private int height;    private int width;    public Rectangle(int aHeight, int aWidth) {    super();    height = aHeight;    width = aWidth;    }    public int getHeight() {    return height;    }    public int getWidth() {    return width;    }    public void setHeight(int aHeight) {    height = aHeight;    }    public void setWidth(int aWidth) {    width = aWidth;    }    public int...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class...

    ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...

  • Templates Apartment.java package hwk7; public class Apartment {    int numOfApartments; // the number of apartments...

    Templates Apartment.java package hwk7; public class Apartment {    int numOfApartments; // the number of apartments of this type    Room[] rooms; // rooms in this type of apartment       Apartment(int numOfApartments, Room[] rooms) {        this.numOfApartments = numOfApartments;        this.rooms = rooms;    }       // Return the window orders for one apartment of this type as TotalOrder object    TotalOrder orderForOneUnit() {        // TODO    }       // Return the window...

  • Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public...

    Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain { public static void main(String[] args) { Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>(); PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated"); PresidentsOfTheUnitedStates.put("John Adams", "Federalist"); PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic"); PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic"); PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig"); PresidentsOfTheUnitedStates.put("John Tyler", "Whig");      } } } Extend given client class: Implement a static method called FilterMapByValue, that takes...

  • departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE =...

    departmentstore: package departmentstorepkg; import java.util.ArrayList; public class DepartmentStore {    private static final int DEFAULT_SIZE = 10; private StaffMember [] myEmployees; private int myNumberEmployees; private String myFileName; private StaffMember[] employee; public DepartmentStore (String filename){ myFileName = filename; myEmployees = employee;    } public String toString(){ return this.getClass().toString() + ": " + myFileName; } public void addEmployee(Employee emp){ } /** * prints out all the employees in the array list held in this class */ public void print(){ for(int i =...

  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class...

    Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " "    Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...

  • Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int...

    Java Do 72a, 72b, 72c, 72d. Code & output required. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[]...

    package array; public class Test { static int[] data = (0,1,2,3,4,5,6,7,8); public static void main (String[] a) { for ( int i = 0;i<data.length; i++) { if(i %3 == 0) { System.out.print("A"); System.out.print(data[i]); System.out.print(" "); } } I need an explanation of what this code is doing ? is there anything wrong with it }}

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT