Frage im Vorstellungsgespräch bei Apple

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

Antworten zu Vorstellungsgespräch

Anonym

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

Anonym

10. Mai 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; }