Tips and tricks

Sorted array
  • Binary search
  • 2 sorted arrays - "merge" operation from the merge sort (e.g. to find closest elements with minimal absolute difference).

Singly-linked list
  • Slow and Fast pointers
    • Finding the middle element
    • Detecting a cycle
  • In-place transformations
    • Inversion (using cur, next, and next_next)
    • Duplication by inserting duplicates after every original item (for fancy linked lists)

Tree

Limited number of math operators
  • Consider several passes over a collection (most likely an array) in different directions (1 -> N and then N -> 1, or vice versa) to accumulate partial results

Prime numbers
  • No divisors other than 1 and itself. Therefore can identify duplicates by accumulating a product of all numbers in a collection - if a number is a divisor for the product, then it's already been examined.
Math
  • Two's compliment of x = ~x + 1. For negative x this is equivalent to -1 * x.
  • A range of integers is given: 0..(N-1)
    • consider using a bitset data structure (e.g. for sorting, provided no duplicates present)
    • Can probably modify the original array where the numbers are stored (e.g. to count them).
    • Use counting sort, replacing +1 with +N at arr[arr[i]%N].
  • If most values are encountered twice (e.g. in a list), we can eliminate them using XOR over all the elements. The result of XOR will contain only those elements that aren't doubled.
String
  • Anagrams - use hashing to detect anagrams. E.g. hash = product of prime numbers associated with every character (a..z map to 2..101) - avoids collisions, but can overflow. Or deal with the collisions later when printing the result.
    http://www.careercup.com/question?id=5743007936544768

No comments:

Post a Comment