Bird
Raised Fist0
Redisquery~20 mins

Why hashes represent objects in Redis - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Hash Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why are hashes used to represent objects in Redis?
In Redis, hashes are often used to represent objects. Why is this approach beneficial compared to using simple strings for each attribute?
ABecause hashes allow grouping multiple fields and values under one key, making it easier to manage related data as a single object.
BBecause hashes automatically encrypt the data stored inside, providing security for objects.
CBecause hashes consume more memory than strings, which improves performance.
DBecause hashes store data in a sorted order, which strings cannot do.
Attempts:
2 left
💡 Hint
Think about how you would store a user profile with multiple details in Redis.
❓ query_result
intermediate
2:00remaining
What does this Redis command output?
Given the following Redis commands executed in order, what is the output of the last command?

HSET user:1 name "Alice" age "30" city "Paris"
HGETALL user:1
Redis
HSET user:1 name "Alice" age "30" city "Paris"
HGETALL user:1
AError: HGETALL command not found
B["user:1", "name", "Alice", "age", "30", "city", "Paris"]
C["name", "Alice"]
D["name", "Alice", "age", "30", "city", "Paris"]
Attempts:
2 left
💡 Hint
HGETALL returns all fields and values in the hash as a list.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this Redis hash command
Which option contains a syntax error when trying to add fields to a Redis hash named 'product:100'?
Redis
HSET product:100 name "Laptop" price 1200 stock 30
AHSET product:100 name "Laptop" price 1200 stock 30
BHSET product:100 name "Laptop" price "1200" stock "30"
CHSET product:100 (name "Laptop" price 1200 stock 30)
DHSET product:100 name Laptop price 1200 stock 30
Attempts:
2 left
💡 Hint
Check the use of parentheses in Redis commands.
❓ optimization
advanced
2:00remaining
Optimizing storage of user profiles in Redis
You want to store 10,000 user profiles in Redis. Each profile has 5 fields: name, email, age, city, and phone. Which approach is most efficient in terms of memory and access speed?
AStore each field of every user as a separate Redis string key, e.g., user:1:name, user:1:email, etc.
BStore each user profile as a Redis hash with one key per user containing all 5 fields.
CStore all user profiles in a single Redis list, each element a JSON string of the profile.
DStore user profiles as Redis sets with each field as a member.
Attempts:
2 left
💡 Hint
Consider grouping related data and efficient retrieval.
🔧 Debug
expert
2:00remaining
Why does this Redis hash command fail to update the field?
You run the following commands:

HSET user:42 name "Bob" age "25"
HSET age 26
HGET user:42 age

But the last command returns "25" instead of "26". What is the most likely reason?
AThe second HSET command is missing the hash key argument, so it does not update the intended hash.
BThe Redis server is caching the old value and did not update the field.
CThe second HSET command is missing quotes around the key, causing it to create a new key instead of updating.
DThe second HSET command is interpreted as setting a new key named 'age' instead of updating the hash.
Attempts:
2 left
💡 Hint
Check the number of arguments in the second HSET command.

Practice

(1/5)
1. Why are Redis hashes considered similar to objects in programming?
easy
A. Because they store multiple related fields under one key, like object properties
B. Because they only store single values like simple variables
C. Because they automatically run code like functions
D. Because they store data in a list format

Solution

  1. Step 1: Understand what a Redis hash stores

    A Redis hash stores multiple field-value pairs under one key, grouping related data.
  2. Step 2: Compare with programming objects

    Programming objects group properties (fields) together, similar to how hashes group fields.
  3. Final Answer:

    Because they store multiple related fields under one key, like object properties -> Option A
  4. Quick Check:

    Hashes = grouped fields like object properties [OK]
Hint: Think of hashes as objects holding many properties [OK]
Common Mistakes:
  • Confusing hashes with simple string keys
  • Thinking hashes run code like functions
  • Assuming hashes store lists instead of field-value pairs
2. Which of the following is the correct Redis command to set a field 'name' with value 'Alice' in a hash 'user:1'?
easy
A. HMSET user:1 name Alice
B. SET user:1 name Alice
C. HGET user:1 name Alice
D. HSET user:1 name Alice

Solution

  1. Step 1: Identify the command to set a single field in a hash

    HSET sets a single field-value pair in a Redis hash.
  2. Step 2: Check other commands

    SET is for strings, HMSET is deprecated, HGET retrieves values.
  3. Final Answer:

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

    Use HSET to set hash fields [OK]
Hint: Use HSET to add or update a single hash field [OK]
Common Mistakes:
  • Using SET instead of HSET for hashes
  • Using HGET which reads but does not set
  • Using deprecated HMSET command
3. Given the Redis commands:
HSET user:2 age 30
HSET user:2 city "New York"
What will HGETALL user:2 return?
medium
A. ["user:2", "age", "30", "city", "New York"]
B. ["age", "30", "city", "New York"]
C. ["age", 30, "city", "New York"]
D. ["age", "city", "30", "New York"]

Solution

  1. Step 1: Understand HGETALL output format

    HGETALL returns an array of field and value strings alternating.
  2. Step 2: Match fields and values

    Fields are "age" and "city" with values "30" and "New York" as strings.
  3. Final Answer:

    ["age", "30", "city", "New York"] -> Option B
  4. Quick Check:

    HGETALL returns field-value pairs as strings [OK]
Hint: HGETALL returns fields and values as string pairs [OK]
Common Mistakes:
  • Including the key name in the output
  • Returning values as numbers instead of strings
  • Mixing field and value order
4. You run the command HSET user:3 name "Bob" age 25 but get an error. What is the likely cause?
medium
A. Field names cannot be strings
B. The key 'user:3' does not exist yet
C. HSET only accepts one field-value pair at a time in this Redis version
D. Age must be a string, not a number

Solution

  1. Step 1: Check HSET command syntax

    Older Redis versions allow only one field-value pair per HSET call.
  2. Step 2: Analyze error cause

    Trying to set two pairs at once causes syntax error in older versions.
  3. Final Answer:

    HSET only accepts one field-value pair at a time in this Redis version -> Option C
  4. Quick Check:

    HSET single pair per call in older Redis [OK]
Hint: Check Redis version for multi-field HSET support [OK]
Common Mistakes:
  • Assuming key must exist before HSET
  • Thinking field names can't be strings
  • Confusing data type requirements for values
5. You want to store user profiles with fields: 'name', 'age', and 'email' in Redis. Which approach best uses hashes to represent these profiles efficiently?
hard
A. Use a separate hash for each user with key like 'user:<id>' and fields for each property
B. Store all users in one big hash with user IDs as fields and JSON strings as values
C. Use simple string keys for each property like 'name:<id>', 'age:<id>', 'email:<id>'
D. Store user data as a list with all properties concatenated as one string

Solution

  1. Step 1: Consider data organization for user profiles

    Each user should have a separate hash to group their properties clearly.
  2. Step 2: Evaluate other options

    One big hash mixes users, separate strings scatter data, lists lose field names.
  3. Final Answer:

    Use a separate hash for each user with key like 'user:<id>' and fields for each property -> Option A
  4. Quick Check:

    Separate hashes per user = clear, efficient object representation [OK]
Hint: Use one hash per object for clear property grouping [OK]
Common Mistakes:
  • Mixing all users in one hash losing clarity
  • Using separate keys for each property causing scattered data
  • Storing data as lists losing field names
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