1. Engineering
  2. Computer Science
  3. package comspartasystemsinterview public class floorplan private final int width...

Question: package comspartasystemsinterview public class floorplan private final int width...

Question details

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));
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution