When two cues meet, or none fits
After this: Combine a count map with a second pass, and know what to do when no cue in the table fits.
On this page · 5 sections · 3 checks
Every problem so far had one cue and one tool. Many interview problems carry two cues, and need two tools, one after the other. Some carry none from the table. This lesson shows both cases.
Two cues: count, then look again
There are two cues. "Appears exactly once" asks how many times each character appears, which is a : a hash map from each character to its count. "The first" asks for a position in the string, and the map holds counts, not positions. So read the string twice: once to count, and once more, in order, to find the first count of 1.
Work it on "swiss":
- First pass:
sis counted 3 times,wonce andionce. - Second pass: index 0 holds
s, count 3. Not it. - Index 1 holds
w, count 1. The answer is 1.
Step through it below: watch the counts box fill in the first loop, then watch i in the second.
- Run the programRun it and step through. In the first loop, watch the counts box fill. In the second loop, watch i move until a count of 1 is found.
def first_unique(text): counts = {} # character -> how many times for ch in text: counts[ch] = counts.get(ch, 0) + 1 for i in range(len(text)): # second pass, in order if counts[text[i]] == 1: return i return -1
print(first_unique("swiss"))run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The first loop adds 1 to the count of each character, starting from 0 for a character not seen yet. The second loop goes through the positions in order and returns the first one whose character has a count of 1. If the loop ends without one, the function returns −1. On "level", l and e appear twice and v once, so the answer is 2.
What does it cost? Two passes over n characters is 2n steps, and 2n grows at the same rate as n, so the time is still O(n). The extra memory is one count for each different character.
Another pair you will meet
"Return the k values that appear most often" also has two cues. "Appear most often" needs a count map, and "the k values with the biggest counts" is a k-largest question, so a heap of size k runs over the counts. Ten classic problems, worked solves it in full.
When no cue fits
Some problems match no row. Then go back to the method from the first interview tutorial: write the slow answer that tries everything, find the part it repeats, and ask what question that part keeps asking. Often the question names a tool after all.
It is also honest to know what this site does not teach yet. Problems whose cue is "number of ways", "fewest coins", "all combinations" or "overlapping intervals" usually need dynamic programming, backtracking, or sorting followed by a greedy choice. The Data structures path does not teach these yet; the memo in Branching recursion is the first step toward dynamic programming. If you meet one, say that you recognise the shape, write the slow answer, and say what you would look up.
Check yourself
A problem says: “Return the k values that appear most often in a list.” Which two tools does it need, in order?
The first-unique function from this lesson, on a new string. What does it print?
def first_unique(text): counts = {} # character -> how many times for ch in text: counts[ch] = counts.get(ch, 0) + 1 for i in range(len(text)): # second pass, in order if counts[text[i]] == 1: return i return -1
print(first_unique("aabb"))import java.util.*;
public class Main { static int firstUnique(String text) { Map<Character, Integer> counts = new HashMap<>(); // character -> how many times for (char ch : text.toCharArray()) { counts.put(ch, counts.getOrDefault(ch, 0) + 1); } for (int i = 0; i < text.length(); i++) { // second pass, in order if (counts.get(text.charAt(i)) == 1) { return i; } } return -1; }
public static void main(String[] args) { System.out.println(firstUnique("aabb")); }}A problem says: “Coins come in values 1, 5 and 6. What is the fewest number of coins that add up to 11?” No row of the cue table fits. What should you do?
Honesty notes
- Combine two tools when a problem has two cues: a count map, then a second pass in order, finds the first character that appears once.
- Say that two passes over the input still cost O(n).
- Name the two tools for top k frequent: a count map, then a heap of size k over the counts.
- When no cue fits, write the slow answer first, find what it repeats, and say honestly which known shape it might be.