problems / 281

Feature Flag Drift Between Environments

easy hashing sorting strings 1000 ms · 256 MB

Feature Flag Drift Between Environments

Before every release, the on-call engineer checks that the feature flags in staging haven't drifted away from production. Each environment exports its flags as lines of key=value. Within one export every key appears at most once, but the two exports may list different keys, in any order.

Write the drift report. It has three sections, in this order:

  • ONLY_STAGING: the flags that exist in staging but not in production.
  • ONLY_PRODUCTION: the flags that exist in production but not in staging.
  • CHANGED: the flags that exist in both environments with different values.

Flags present in both environments with the same value are not reported.

Keys and values are case-sensitive: Beta and beta are different keys, and on and ON are different values.

Input

The first line contains n (0 ≤ n ≤ 10^5), the number of flags in staging. Each of the next n lines contains one staging flag as key=value.

The next line contains m (0 ≤ m ≤ 10^5), the number of flags in production, followed by m lines in the same format.

Every key and every value is a non-empty string of at most 50 characters, each being an English letter (a–z, A–Z), a digit, _, . or -. So each line contains exactly one =, with no spaces. The keys within one environment are pairwise distinct.

Output

For each section, print a heading line with the section name and the number of flags in it, then one line per flag:

  • In ONLY_STAGING and ONLY_PRODUCTION, print the flag as key=value, exactly as it appears in its environment.
  • In CHANGED, print key staging_value -> production_value.

Within every section, list the flags in increasing order of key, comparing keys character by character by their ASCII codes (so digits come before uppercase letters, which come before lowercase letters). A section with no flags is just its heading with the count 0.

Notes

In the first example, checkout_v2 is only in staging, legacy_banner is only in production, max_upload_mb changed from 25 to 10, and dark_mode is identical in both, so it is not reported.

In the second example, the keys Beta and beta are different flags. In ASCII order, uppercase letters come before lowercase ones, so Beta is listed before beta. Staging is empty, so ONLY_STAGING and CHANGED have no flags.

Example 1

Input

3
max_upload_mb=25
dark_mode=on
checkout_v2=true
3
dark_mode=on
legacy_banner=enabled
max_upload_mb=10

Output

ONLY_STAGING 1
checkout_v2=true
ONLY_PRODUCTION 1
legacy_banner=enabled
CHANGED 1
max_upload_mb 25 -> 10

Example 2

Input

0
3
beta=on
Beta=off
api.timeout-ms=3000

Output

ONLY_STAGING 0
ONLY_PRODUCTION 3
Beta=off
api.timeout-ms=3000
beta=on
CHANGED 0

Hints · reveal one at a time if you're stuck

Hint 1

Comparing every staging flag against every production flag is up to 10^10 comparisons. What structure answers "does production have this key, and with what value?" instantly?

Hint 2

Store each environment in a hash map from key to value: one lookup per key then tells you which section a flag belongs to. A hash map keeps no order, so think about what still has to happen before printing, and don't change the case of keys or values.

report an issue Prefer your own editor? sooks prep 281 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.