Hashes, Lists & Sorted Sets — Series 3

Preview — 3 of 10 questions

Given a hash user:1000 with 5 fields, what does HRANDFIELD user:1000 2 return?

A2 distinct field names chosen at random from the hash, with no duplicates — a positive count means "up to N distinct fields," capped at however many actually exist; a negative count instead allows the same field to be picked more than once
BThe 2 fields with the alphabetically first names
CThe 2 most recently modified fields
DA single random field, repeated twice in the result array

Given RPUSH tasks "a" "b" "c" "b" "d", what does LPOS tasks "b" return, and how would you find every occurrence?

ALPOS tasks "b" returns 1 — the index of the first matching occurrence (0-indexed). To find every occurrence, LPOS tasks "b" COUNT 0 returns all matching indexes ([1, 3] here) — COUNT 0 specifically means "no limit," rather than "zero results"
BLPOS only returns a boolean (true/false) indicating whether the element exists anywhere in the list
CLPOS always returns every matching index by default, with no way to limit it to just the first
DLPOS requires the list to be sorted first, or it raises an error

Modern Redis (6.2+) consolidated several separate sorted-set range commands into one. What replaced ZREVRANGE and ZRANGEBYSCORE?

ANothing — ZREVRANGE and ZRANGEBYSCORE remain the only way to do reverse or score-based ranges
BZRANGE was renamed to ZSCAN for all range-based queries
CZRANGE itself, extended with optional modifiers: REV for reverse order (replacing ZREVRANGE), and BYSCORE/BYLEX to range by score or lexicographic value instead of by rank/index (replacing ZRANGEBYSCORE/ZRANGEBYLEX) — one unified command covering what previously required several differently-named ones, each with their own slightly different argument conventions
DThe separate commands were merged, but only for cluster deployments; standalone Redis still requires the old separate commands

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.