Chapter 16 - Moderate
In this chapter, we discuss some moderate-level questions.
Exercises
16.1 Number Swapper
a = a + b;
b = a - b;
a = a - b;
16.2 Word Frequencies
Use a hash table.
16.3 Intersection
This is a mathematical question. Two infinite lines will always intersect unless they are parallel or the same line. We can compute their respective gradient to check this.
16.4 Tic Tac Win
If we call the function many times, we probably shall hard-code all winning conditions using a bit vector.
16.5 Factorial Zeros
Each trailing 0 means a factor of 10, which means a pair of 2 and 5. We can use two separate procedures to count the number of factors of 2s and 5s. Note that 25 counts for 2 factors of 5s.
16.6 Smallest Differences
- Sort two arrays and do the merging process.
- Store one of them into a hash table.
16.7 Number Max
Use bit operation instead.
16.8 English Int
Use a simple bit-wise iteration. Remember negative sign. Be careful.
16.9 Operations
Use add to implement them. To implement negate
, use a while loop with the value of the absolute value as the loop counter.