← Questions

Report a user's license usage from a log file

0
AmazonCodingLast asked Sep 2025

A coding interview question asked in Amazon's technical phone screen, last asked Sep 2025. It tests shell, parsing and hashing.

Write a command-line script that reports one user's license usage from a log file, then make repeated lookups fast with a cache.

Part 1: the script

Unless you agree on a different interface with the interviewer, assume:

  • Called as ./script.sh <username> <log_file>.
  • Each log line is timestamp license user@hostname, for example 2026-03-01T09:00:00 matlab ana@build-07.
  • Output: one line per license the user used, with the number of uses and the hosts it was used on, sorted by license name.
  • Errors: a usage message and exit code 1 for a wrong number of arguments; a clear message and exit code 2 if the log file does not exist or cannot be read; malformed lines skipped and counted on standard error.
$ ./script.sh ana usage.log
autocad 1 build-03
matlab 2 build-07,laptop-ana

for a log holding matlab ana@build-07, matlab ana@laptop-ana, autocad ana@build-03 and matlab bo@build-07 lines.

Part 2: a cache

The script now runs many lookups against the same logs (or the expensive step, such as parsing the log or a license string, repeats with the same inputs). Add an in-memory cache backed by a hash map and explain:

  • Where caching helps: a pure, deterministic computation whose inputs recur, and which calls should not be cached.
  • The key (normalised: canonical file path, trimmed and lower-cased license name) and the value.
  • The expected hit rate from the number of distinct inputs against the number of lookups.
  • An eviction policy (none, LRU, LFU or a TTL) for the working set and the memory budget.
  • Invalidation: re-check the file's modification time and size, or its content hash, on each read, or accept a TTL; the trade-off between freshness and speed.
  • Concurrency: races between threads that miss at the same time, a lock per key to stop a stampede on a hot miss, and what changes with several processes.
  • get / put pseudocode with the validity check, and the time and space before and after.

Clarifying questions

  • Exactly what should the report show: licenses, counts, hosts, time ranges?
  • Is the username matched exactly, and case-sensitively?
  • How large are the log files, and how often do they change?
  • Bash only, or may the script call awk or Python?

What a strong answer covers

  • One pass over the file (awk or a small parser) that filters on the user field, builds counts per license and the set of hosts, and prints sorted output; O(lines) time and O(licenses × hosts) memory.
  • Argument checking, quoting of variables, and exit codes callers can rely on.
  • For Part 2, an index built once from the log (user to licenses), turning each lookup from O(lines) into O(1) on a hit, with the file's modification time as the validity check.

Follow-ups

  • Report the peak number of licenses in use at once, per license.
  • The log is rotated daily into compressed files. Read across them.
  • Several copies of the script run at once. Share the cache between them.

Where it's asked

  • Amazon · Technical phone screen · Last asked Sep 202560 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

shell · parsing · hashing

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding