fork download
  1. ; three amazon interview questions
  2.  
  3. (define (ransom note magazine)
  4. (let ((alphabet (make-vector 256 0)))
  5. (do ((note (string->list note) (cdr note))) ((null? note))
  6. (vector-set! alphabet (char->integer (car note))
  7. (+ (vector-ref alphabet (char->integer (car note))) 1)))
  8. (do ((mag (string->list magazine) (cdr mag))) ((null? mag))
  9. (vector-set! alphabet (char->integer (car mag))
  10. (- (vector-ref alphabet (char->integer (car mag))) 1)))
  11. (not (positive? (apply max (vector->list alphabet))))))
  12.  
  13. (display (ransom "send money" "four score and seven years ago")) (newline)
  14. (display (ransom "send money" "four million and seven years ago")) (newline)
  15.  
  16. (define (consec xs)
  17. (let loop ((xs xs) (prev #f) (run 1) (maxval #f) (maxcnt 0))
  18. (cond ((null? xs) (values maxval maxcnt))
  19. ((equal? (car xs) prev)
  20. (if (<= maxcnt run)
  21. (loop (cdr xs) prev (+ run 1) prev (+ run 1))
  22. (loop (cdr xs) prev (+ run 1) maxval maxcnt)))
  23. (else (loop (cdr xs) (car xs) 1 maxval maxcnt)))))
  24.  
  25. (call-with-values
  26. (lambda () (consec '(1 1 1 2 3 3 3 3 4 4 1 1 1)))
  27. (lambda (val cnt)
  28. (display val) (newline)
  29. (display cnt) (newline)))
Success #stdin #stdout 0.01s 8800KB
stdin
Standard input is empty
stdout
#f
#t
3
4