Problem Solving with Algorithms

CoderPad

2020. 10. 12. 23:05
반응형
/* This is an example pad with a question attached. This question is an example of how you can use the pad during an interview with a candidate.

You can play around, type and run code as needed. Once you click on View Playback in the Settings in the bottom right hand corner, you will be able to view the playback of this pad and what was typed during the interview.
--------------------------------------------------------
Design a parking lot using object-oriented principles

Goals:
- Your solution should be in Java - if you would like to use another language, please let the interviewer know.
- Boilerplate is provided. Feel free to change the code as you see fit

Assumptions:
- The parking lot can hold motorcycles, cars and vans
- The parking lot has motorcycle spots, car spots and large spots
- A motorcycle can park in any spot
- A car can park in a single compact spot, or a regular spot
- A van can park, but it will take up 3 regular spots
- These are just a few assumptions. Feel free to ask your interviewer about more assumptions as needed

Here are a few methods that you should be able to run:
- Tell us how many spots are remaining
- Tell us how many total spots are in the parking lot
- Tell us when the parking lot is full
- Tell us when the parking lot is empty
- Tell us when certain spots are full e.g. when all motorcycle spots are taken
- Tell us how many spots vans are taking up

Hey candidate! Welcome to your interview. I'll start off by giving you a Solution class. To run the code at any time, please hit the run button located in the top left corner.
*/

import java.io.*;
import java.util.*;

class Solution {
  public static void main(String[] args) {
    ArrayList<String> strings = new ArrayList<String>();
    strings.add("Hello, World!");
    strings.add("Please put code below");
    for (String string : strings) {
      System.out.println(string);
    }
  }
}

 

 

 


 

 

Java

Running OpenJDK 11 - Autocomplete is enabled

You should define a public class named Solution with a public static void main. Your code is compiled with -Xlint (linting) and run with -ea (assertions enabled).

A few libraries are included for your convenience, and are available on the classpath with no additional work from you. Simply import and fire away:

The google code project page has some useful examples.

Has a bunch of useful stuff like date parsing. The import prefix is org.apache.commons.lang3, so you can perform imports by writing import org.apache.commons.lang3.ArrayUtils.

  • JUnit, the gold standard for testing in Java. If you want to ask your candidate to write JUnit-style tests during the interview, please format your Java code like so:

import org.junit.*; import org.junit.runner.*; public class Solution { @Test public void testNoop() { Assert.assertTrue(true); } public static void main(String[] args) { JUnitCore.main("Solution"); } }

Stack traces originating from JUnit errors are trimmed for brevity.

In the future we may auto-detect the existence of @Test annotations, so stay tuned.

  • jMock, a library to assist with mocking in Java. Combining jMock with the previous JUnit example:

import org.jmock.*; import org.junit.*; import org.junit.runner.*; interface Receiver { void receive(String message); } public class Solution { @Test public void testMock() { Mockery context = new Mockery(); Receiver receiver = context.mock(Receiver.class); context.checking(new Expectations() {{ oneOf (receiver).receive("hello"); }}); receiver.receive("hello"); context.assertIsSatisfied(); } public static void main(String[] args) { JUnitCore.main("Solution"); } }

Are there any libraries or settings we missed? Feel free to email us with suggestions!

 

 

 

JUnit

  import org.junit.*;
  import org.junit.runner.*;

  public class Solution {
    @Test
    public void testNoop() {
      Assert.assertTrue(true);
    }

    public static void main(String[] args) {
      JUnitCore.main("Solution");
    }
  }

 

 

 

JMock

  import org.jmock.*;
  import org.junit.*;
  import org.junit.runner.*;

  interface Receiver {
    void receive(String message);
  }

  public class Solution {
    @Test
    public void testMock() {
      Mockery context = new Mockery();
      Receiver receiver = context.mock(Receiver.class);
      context.checking(new Expectations() {{
        oneOf (receiver).receive("hello");
      }});
      receiver.receive("hello");
      context.assertIsSatisfied();
    }

    public static void main(String[] args) {
      JUnitCore.main("Solution");
    }
  }

 

 

 

coderpad.io/

반응형
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band