Samsara 店面面经整理

找来找去就一道题,OOD设计货物和货车的class
Glassdoor上也有相关描述:
You start off with a straightforward OOP question of designing a class with a constructor.
The company makes sensors for shipping and trucks so the class was for a Trailer and a Container.
You do some simple functions related to adding containers to the trailer,
and then you to a function where they want to know how to determine a Trailer’s weight at any given time.
Write a load(), unload() and weight() function .

试着写了一下

代码
import java.util.*;

class Container {
		private double weight;
		private int id;

		public Container(int id, double weight) {
			this.weight = weight;
			this.id = id;
		}

		public double weight() {
			return this.weight;
		}

		public int getId() {
			return this.id;
		}
}

class Trailer {
		private Map<Integer, Container> cMap;
		private Map<Long, Double> timeWtMap;
		private double total; 

		public Trailer() {
			total = 0;
			cMap = new HashMap<>();
			timeWtMap = new TreeMap<>();
		}

		public void load(Container ct) {
			cMap.put(ct.getId(), ct);
			total += ct.weight();
			long timeNow = System.currentTimeMillis();
			timeWtMap.put(timeNow, total);
		}

		public Container unload(int id) {
			Container ct = cMap.get(id);
			cMap.remove(id);
			long timeNow = System.currentTimeMillis();
			total -= ct.weight();
			timeWtMap.put(timeNow, total);
			return ct;
		}

		public double totalWeight() {
			return this.total;
		}

		public double getTotalWeight(long timestamp) {
			double sum = 0;
			for (long time : timeWtMap.keySet()) {
				if (time <= timestamp) {
					sum = timeWtMap.get(time);
				} else {
					System.out.println("not yet: " + new Date(time));
					return sum;
				}
			}
			return sum;
		}

		public int size() {
			return cMap.size();
		}
}

class TMS {
	
	public static void main(String[] args) {
		Container ct1 = new Container(1, 10.0);
		Container ct2 = new Container(2, 20.0);
		Trailer t = new Trailer();
		t.load(ct1);
		long timeNow1 = System.currentTimeMillis();
		System.out.println(new Date(timeNow1));
		System.out.println("current total weight: " + t.totalWeight());
		System.out.println("current number of containers: " + t.size());
		//sleep for a second
		try
		{
		    Thread.sleep(1000);
		}
		catch(InterruptedException ex)
		{
		    Thread.currentThread().interrupt();
		}
		t.load(ct2);
		long timeNow2 = System.currentTimeMillis();
		System.out.println(new Date(timeNow2));
		System.out.println("current total weight: " + t.totalWeight());
		System.out.println("current number of containers: " + t.size());
		
		//sleep for a second
		try
		{
		    Thread.sleep(1000);
		}
		catch(InterruptedException ex)
		{
		    Thread.currentThread().interrupt();
		}
		t.unload(2);
		long timeNow3 = System.currentTimeMillis();
		System.out.println(new Date(timeNow3));
		System.out.println("current total weight: " + t.totalWeight());
		System.out.println("current number of containers: " + t.size());

		long timeNow = System.currentTimeMillis();
		System.out.println(new Date(timeNow));
		System.out.println(t.getTotalWeight(timeNow));

		long timeBefore = timeNow - 800;
		System.out.println(new Date(timeBefore));
		System.out.println(t.getTotalWeight(timeBefore));
	}
}

输出

Wed Sep 11 12:55:23 EDT 2019
current total weight: 10.0
current number of containers: 1
Wed Sep 11 12:55:25 EDT 2019
current total weight: 30.0
current number of containers: 2
Wed Sep 11 12:55:26 EDT 2019
current total weight: 10.0
current number of containers: 1
Wed Sep 11 12:55:26 EDT 2019
10.0
Wed Sep 11 12:55:25 EDT 2019
not yet: Wed Sep 11 12:55:26 EDT 2019
30.0

刚面试完,正好碰到这题。不过不需要调用System.currentTimeMillis(), load/unload method直接加一个时间参数。
面试官人不错,不浪费时间上来就让做题,偶尔有小错误也及时指出

拿到onsite了吗

杳无音讯……

祝好运哈!想问下你面试完后看你这个原来写的代码 感觉还有什么要change的吗?然后这个题目我其实不是很懂 能详细的再描述一下吗 这里的container指的是什么东西?

这题你有个大概印象和思路就好,如果你面试碰到这题面试官会和你澄清题意的,而且可能细节会变,最好不要先入为主。
要改的地方就是load(), unload(), getWeightAt()三个method最后都会输入时间参数,所以代码会更简单,不需要专门去获取当前时间。
container你可以理解成一个集装箱或者一个基本货物单元,有id和重量两个变量。

好的非常感谢你! 我第一次电面 所以不知道应该问什么问题 怎么保持交流?有什么建议吗

大概就是把你脑子里想的思路说出来,让面试官知道你在写什么,然后对于不清楚的requirement及时问

2 个帖子已被合并到了现有主题:Samsara OA

非常感谢大哥啊!

想问下你收到samsara的电面是过了多久?我周日sign up了calendar 但是一直没有人回复我约面试 是不是凉了?

不一定 再等等 我等了三天左右

楼主拿到onsite了吗

没信

再来followup一下:想问下楼主当时怎么测试的 因为没有用过codepad那个平台 不是很了解 是直接run main吗?

然后问下followup问题你还记得吗?

帖子里的代码直接terminal里面用javac compile然后java 运行就可以。面试会用code pair,直接在软件里就可以编译运行

问到给定时间获取当前重量这一问就结束了

好的 真的非常感激楼主回复这么快!嗯嗯 能直接编译就好!你最后拿到onsite了吗?祝愿你好运!

我看了获取时间的重量follow up是不是要求优化一下?用二分法。