Bird
Raised Fist0
Redisquery~20 mins

Object storage with hashes in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Hash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
Retrieve a field from a Redis hash
Given a Redis hash stored with key user:1001 containing fields name, email, and age, what is the output of the command HGET user:1001 email if the stored email is alice@example.com?
Redis
HSET user:1001 name Alice email alice@example.com age 30
HGET user:1001 email
A"alice@example.com"
B"Alice"
C30
Dnil
Attempts:
2 left
💡 Hint
Remember that HGET returns the value of the specified field in the hash.
📝 Syntax
intermediate
1:30remaining
Identify the correct Redis command to add multiple fields to a hash
Which of the following commands correctly adds multiple fields title and author to a Redis hash with key book:1?
AHMSET book:1 title "Redis Basics" author "John Doe"
BHADD book:1 title "Redis Basics" author "John Doe"
CHSET book:1 {title: "Redis Basics", author: "John Doe"}
DHSET book:1 title "Redis Basics" author "John Doe"
Attempts:
2 left
💡 Hint
Use the modern command that supports multiple field-value pairs.
❓ optimization
advanced
2:00remaining
Efficiently retrieving multiple fields from a Redis hash
You want to retrieve the fields name, email, and age from a Redis hash user:1002. Which command is the most efficient to get all three fields in a single call?
AHMGET user:1002 name email age
BHGETALL user:1002
C
HGET user:1002 name
HGET user:1002 email
HGET user:1002 age
DGET user:1002
Attempts:
2 left
💡 Hint
Choose the command that fetches specific fields in one request.
🔧 Debug
advanced
1:30remaining
Identify the error in this Redis hash command
What error will the following Redis command produce?
HSET user:1003 name "Bob" age
Redis
HSET user:1003 name "Bob" age
ASets 'age' field to the string 'age'
BSets 'age' field to empty string
CSyntax error: wrong number of arguments for 'HSET' command
DNo error, command runs successfully
Attempts:
2 left
💡 Hint
HSET requires field-value pairs; check if all pairs are complete.
🧠 Conceptual
expert
2:30remaining
Understanding Redis hash memory efficiency
Why are Redis hashes considered memory efficient for storing many small objects compared to storing each object as a separate key?
ABecause hashes allow storing only string values, which are smaller
BBecause hashes internally use a ziplist or hashtable optimized for small fields, reducing overhead per field
CBecause hashes store data on disk instead of memory
DBecause Redis compresses all hash keys automatically
Attempts:
2 left
💡 Hint
Think about how Redis stores small hashes internally.

Practice

(1/5)
1. What does a Redis hash store?
easy
A. A set of unique keys without duplicates
B. Only a single string value per key
C. A list of keys without values
D. Multiple fields and their values under one key

Solution

  1. Step 1: Understand Redis hash structure

    Redis hashes store multiple field-value pairs grouped under a single key.
  2. Step 2: Compare options with hash definition

    Only Multiple fields and their values under one key correctly describes storing multiple fields and values under one key.
  3. Final Answer:

    Multiple fields and their values under one key -> Option D
  4. Quick Check:

    Hashes = multiple fields + values [OK]
Hint: Hashes group fields and values under one key [OK]
Common Mistakes:
  • Thinking hashes store only one value
  • Confusing hashes with lists or sets
  • Assuming keys are stored without values
2. Which Redis command correctly adds a field 'name' with value 'Alice' to a hash key 'user:1'?
easy
A. HGET user:1 name Alice
B. HSET user:1 name Alice
C. SET user:1 name Alice
D. HMGET user:1 name Alice

Solution

  1. Step 1: Identify command to add/update hash field

    HSET is the Redis command to set a field in a hash.
  2. Step 2: Check command syntax

    HSET key field value is correct; so HSET user:1 name Alice is valid.
  3. Final Answer:

    HSET user:1 name Alice -> Option B
  4. Quick Check:

    HSET adds/updates hash fields [OK]
Hint: Use HSET to add or update hash fields [OK]
Common Mistakes:
  • Using HGET instead of HSET to add data
  • Using SET which is for strings, not hashes
  • Confusing HMGET (retrieves multiple fields) with HSET
3. Given the following Redis commands executed in order:
HSET user:2 age 30
HSET user:2 name Bob
HGETALL user:2
What is the output of HGETALL user:2?
medium
A. ["age", 30, "name", "Bob"]
B. ["user:2", "age", "30", "name", "Bob"]
C. ["age", "30", "name", "Bob"]
D. ["name", "Bob"]

Solution

  1. Step 1: Understand HSET commands

    HSET adds fields 'age' with '30' and 'name' with 'Bob' to hash 'user:2'.
  2. Step 2: Understand HGETALL output format

    HGETALL returns an array of field-value pairs as strings in order added.
  3. Final Answer:

    ["age", "30", "name", "Bob"] -> Option C
  4. Quick Check:

    HGETALL returns all fields and values as string pairs [OK]
Hint: HGETALL returns flat list of field-value strings [OK]
Common Mistakes:
  • Expecting key name in output
  • Mixing data types (number vs string)
  • Only getting one field instead of all
4. You run the command HGET user:3 email but get a null response. What could be the reason?
medium
A. The field 'email' does not exist in the hash 'user:3'
B. The key 'user:3' is a string, not a hash
C. The Redis server is down
D. The command syntax is incorrect

Solution

  1. Step 1: Understand HGET behavior

    HGET returns null if the field does not exist in the hash.
  2. Step 2: Check other options

    Key being a string would cause an error, not null; server down or syntax error would cause different errors.
  3. Final Answer:

    The field 'email' does not exist in the hash 'user:3' -> Option A
  4. Quick Check:

    Missing field in hash returns null [OK]
Hint: Null means field missing, not syntax or server error [OK]
Common Mistakes:
  • Assuming null means server or syntax error
  • Confusing key type errors with missing field
  • Expecting error instead of null for missing field
5. You want to store user profiles with fields 'name', 'age', and 'email' in Redis hashes. Which approach is best to retrieve all user data efficiently?
hard
A. Store each user as a separate hash with key 'user:<id>' and use HGETALL to get all fields
B. Store all users in one hash with fields like 'user1_name', 'user1_age', 'user2_name', etc.
C. Store each field as a separate key like 'name:<id>', 'age:<id>', 'email:<id>'
D. Store user data as JSON strings in simple keys

Solution

  1. Step 1: Analyze storage options

    Storing each user as a separate hash groups related fields logically and allows efficient access.
  2. Step 2: Evaluate retrieval efficiency

    Using HGETALL on 'user:<id>' fetches all fields at once, simpler and faster than scattered keys or complex parsing.
  3. Final Answer:

    Store each user as a separate hash with key 'user:<id>' and use HGETALL to get all fields -> Option A
  4. Quick Check:

    Separate hashes per user = organized and efficient [OK]
Hint: Use one hash per user for grouped fields and easy retrieval [OK]
Common Mistakes:
  • Mixing all users in one hash causing complex keys
  • Using separate keys for each field complicates retrieval
  • Storing JSON loses Redis hash benefits
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard