Final challenge: a practice interview
After this: Restate, compare two answers, complete the code, test the edges and say the cost for a new problem.
This challenge is a short practice interview on a problem you have not seen in this tutorial, valid anagram: are two words made of the same letters, each used the same number of times? For example, listen and silent are anagrams, and rat and car are not.
It has five steps, one for each habit, and each step is scored on its own. You will restate the problem and use the answer to a question, compare two answers and their costs, complete the code, test the edge cases, and choose the best sentence to say about the cost. Every step shows the code in Python and in Java. You can try as many times as you like, and hints work as they do in the lessons. Say your answers out loud as you go: that is the practice.
You restate the problem and ask: "Do a capital D and a small d count as the same letter?" The interviewer says no, they are different letters. With that rule, are Dusty and study anagrams?
One answer sorts the letters of both words and compares the results. The other counts the letters of the first word in a map, then takes them off for the second. What are their time costs, for words of n letters?
Complete the condition: when a letter of the second word has no count left to take off, the words are not anagrams. With it, the program prints True False Falsetrue false false.
def is_anagram(a, b): if len(a) != len(b): return False counts = {} for ch in a: counts[ch] = counts.get(ch, 0) + 1 for ch in b: ____________________ # ← is there one of ch left to use? return False counts[ch] -= 1 return True
print(is_anagram("listen", "silent"), is_anagram("rat", "car"), is_anagram("Dusty", "study"))import java.util.*;
public class Main { static boolean isAnagram(String a, String b) { if (a.length() != b.length()) { return false; } Map<Character, Integer> counts = new HashMap<>(); for (char ch : a.toCharArray()) { counts.put(ch, counts.getOrDefault(ch, 0) + 1); } for (char ch : b.toCharArray()) { ____________________ // ← is there one of ch left to use? return false; } counts.put(ch, counts.get(ch) - 1); } return true; }
public static void main(String[] args) { System.out.println(isAnagram("listen", "silent") + " " + isAnagram("rat", "car") + " " + isAnagram("Dusty", "study")); }}Three edge cases: two empty words, words of different lengths, and the same letters used a different number of times. What does this print?
def is_anagram(a, b): if len(a) != len(b): return False counts = {} for ch in a: counts[ch] = counts.get(ch, 0) + 1 for ch in b: if counts.get(ch, 0) == 0: return False counts[ch] -= 1 return True
print(is_anagram("", ""), is_anagram("a", "ab"), is_anagram("aab", "abb"))import java.util.*;
public class Main { static boolean isAnagram(String a, String b) { if (a.length() != b.length()) { return false; } Map<Character, Integer> counts = new HashMap<>(); for (char ch : a.toCharArray()) { counts.put(ch, counts.getOrDefault(ch, 0) + 1); } for (char ch : b.toCharArray()) { if (counts.getOrDefault(ch, 0) == 0) { return false; } counts.put(ch, counts.get(ch) - 1); } return true; }
public static void main(String[] args) { System.out.println(isAnagram("", "") + " " + isAnagram("a", "ab") + " " + isAnagram("aab", "abb")); }}You finish the counting answer. Which sentence is the best summary to say to the interviewer?
- Run a short practice interview on valid anagram: restate and ask, compare sorting (O(n log n)) with counting (O(n)), complete the count check, test three edge cases, and say the cost in one sentence.