Pergunta de entrevista da empresa Amazon

-Few language specific questions on Java -- what are private/public/package, who can access variable and methods given one of these? -What is a hashtable, how would you implement one? -Binary trees/AVL trees, what can you tell me about them. -Given two texts, x and y, where x < y, check if all the characters of x are in y. -Given a string, find the longest repeating substring.

Resposta da entrevista

Sigiloso

8 de dez. de 2018

import java.util.HashMap; import java.util.Map; public class duplicateCharacters { public static void main(String[] args) { String st1 = "test"; Map st1HM = new HashMap(); st1HM = getCharHM(st1.toCharArray()) ; Map.Entry maxEntry = null; for (Map.Entry entry : st1HM.entrySet() ) { if ( maxEntry == null || entry.getValue() > maxEntry.getValue() ) { maxEntry = entry; } } System.out.println(maxEntry); } public static Map getCharHM (char[] charArray) { Map charArrayHM = new HashMap(); int curValue; for (char x : charArray) { if (charArrayHM.containsKey(x)) { curValue = charArrayHM.get(x); curValue++; } else { curValue = 1; } charArrayHM.put(x,curValue); } return charArrayHM; } }