Pregunta de entrevista de Apple

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

Respuestas de entrevistas

Anónimo

30 abr 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.

Anónimo

10 may 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; }