Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;

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 largestNBuildings(Map 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 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 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 expected = Collections.unmodifiableList(Arrays.asList("b10", "b7", "b2", "b5"));

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions