Apple Interview Question

Implement a peek using a existing iterator next and hasnext function. Interviewer was interested in correct syntax.

Interview Answers

Anonymous

Apr 30, 2016

simple boolean and temp variable to hold the front element. Boolean value to check whether element is set or not (peek has been invoked or not). It can also be implemented without boolean with just temp variable then we need to check for null ness instead of boolean value.

Anonymous

May 10, 2016

int mostFrequentElement(int[] nums) { if (nums.length == 0) return nums[0]; Map map = new HashMap(); for (int n : nums) map.put(n, map.getOrDefault(n, 0) + 1); int count = 0; int maxValue = 0; for (Integer i : map.keySet()) { if (map.get(i) > count) { count = map.get(i); maxValue = i; } } return maxValue; }