微软实习面经

第一题 Tell me about an experience about how your collaborate with your colleagues? (Be specific)

第二题 Write a function to print the numbers from 1 to 100, when number % 3 == 0, print “Three”, when number % 5 == 0, print “Five”, otherwise print the number.
eg.
1–1
2–2
3–Three
4–4
5–Five
6–Three
.
.
.
15–Three Five

public static void main(String[] args){
	for(int i = 1; i <= 100; i ++){
		System.out.print(print(i));
		System.out.println();
	}
}

private String print(int n){
	if(n % 3 == 0)
		return "Three";
	if(n % 5 == 0)
		return "Five";
	else
		return String.valueOf(n);
}

follow up, how to test this function? (I am not sure what this question means.)

第三题:What’s the difference between Graph and Tree?
follow up, write a function to determine whether a Graph is tree or not?

class Node{
	public int val;
	public List<Node> neighbors;
	public Node(){}
	public Node(int _val, List<Node> _neighbors){
		val = _val;
		neighbors = _neighbors;
	}
};

public boolean isTree(Node node){
	Set<Node> set = new HashSet<>();
	Stack<Node> st = new Stack<>();
	st.push(node);
	while(!st.empty()){
		node = st.pop();
		if(set.contains(node)) return false;
		set.add(node);
		for(Node next : node.neighbors){
			if(next != null)
				st.push(next);
		}
	}
	return true;
}
1 Like