Bird
Raised Fist0
Redisquery~20 mins

Memory-efficient data structures in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Redis command sequence?
Consider the following Redis commands executed in order:

HMSET user:1 name "Alice" age 30 city "NY"
HGETALL user:1

What is the output of HGETALL user:1?
Redis
HMSET user:1 name "Alice" age 30 city "NY"
HGETALL user:1
A["user:1", "name", "Alice", "age", "30", "city", "NY"]
B{"name": "Alice", "age": 30, "city": "NY"}
C["name", "Alice", "age", "30", "city", "NY"]
D["name", "Alice", "age", 30, "city", "NY"]
Attempts:
2 left
💡 Hint
Remember that HGETALL returns a flat list of fields and values as strings.
🧠 Conceptual
intermediate
2:00remaining
Which Redis data structure is most memory-efficient for storing a large set of unique integers?
You need to store millions of unique integers in Redis with minimal memory usage. Which data structure should you choose?
ARedis Bitmap
BRedis Sorted Set
CRedis Set
DRedis List
Attempts:
2 left
💡 Hint
Think about bit-level storage for integers.
📝 Syntax
advanced
2:00remaining
Which Redis command correctly creates a memory-efficient small hash?
You want to create a small hash with fields 'a' and 'b' having values '1' and '2' respectively, optimized for memory. Which command is correct?
AHSET myhash {a 1} {b 2}
BHSET myhash a 1 b 2
CHMSET myhash a 1 b 2
DHSET myhash a=1 b=2
Attempts:
2 left
💡 Hint
Check the correct syntax for HSET with multiple fields.
❓ optimization
advanced
2:00remaining
How to optimize memory usage for a Redis list with many small elements?
You have a Redis list with many small string elements. Which approach reduces memory usage the most?
AUse a Redis List as is
BUse Redis Sorted Set instead
CStore elements as separate keys
DConvert the list to a Redis Ziplist-encoded list by keeping it small
Attempts:
2 left
💡 Hint
Redis uses special encodings for small lists to save memory.
🔧 Debug
expert
2:00remaining
Why does this Redis command cause a memory spike?
You run:
LPUSH mylist "a" "b" "c" ... "z"
and notice a sudden memory spike. What is the most likely cause?
Redis
LPUSH mylist "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
ARedis converted the list from Ziplist to Linkedlist due to size
BLPUSH command syntax is incorrect causing memory leak
CRedis is duplicating the list elements internally
DRedis is compressing the list causing temporary memory spike
Attempts:
2 left
💡 Hint
Redis changes internal encoding based on list size.

Practice

(1/5)
1. Which Redis data structure is best suited for storing many small key-value pairs efficiently in memory?
easy
A. List
B. Set
C. Hash
D. String

Solution

  1. Step 1: Understand Redis data structures

    Hashes store multiple field-value pairs under one key, saving memory compared to many separate keys.
  2. Step 2: Compare with other structures

    Lists and sets store collections but are less memory-efficient for many small pairs; strings store single values only.
  3. Final Answer:

    Hash -> Option C
  4. Quick Check:

    Hashes = memory-efficient key-value pairs [OK]
Hint: Use hashes for many small key-value pairs to save memory [OK]
Common Mistakes:
  • Choosing List or Set for key-value pairs
  • Thinking String can store multiple pairs
  • Ignoring memory efficiency of Hashes
2. Which of the following is the correct Redis command to add a field-value pair to a hash named user:1?
easy
A. HSET user:1 name "Alice"
B. SADD user:1 name "Alice"
C. LPUSH user:1 name "Alice"
D. SET user:1 name "Alice"

Solution

  1. Step 1: Identify the command for hashes

    HSET adds or updates a field in a hash key in Redis.
  2. Step 2: Check other commands

    SADD is for sets, LPUSH for lists, SET for strings; none add fields to hashes.
  3. Final Answer:

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

    HSET = add field to hash [OK]
Hint: Use HSET to add fields to hashes in Redis [OK]
Common Mistakes:
  • Using SADD or LPUSH for hashes
  • Confusing SET with HSET
  • Incorrect command syntax
3. Given the Redis commands:
HSET user:2 age 30
HSET user:2 city "NYC"
What will HGETALL user:2 return?
medium
A. ["age", "30", "city", "NYC"]
B. ["user:2", "age", "30", "city", "NYC"]
C. ["age", 30, "city", "NYC"]
D. ["age", "30", "city"]

Solution

  1. Step 1: Understand HSET commands

    Two fields, 'age' and 'city', are set with values '30' and 'NYC' in hash 'user:2'.
  2. Step 2: Check HGETALL output format

    HGETALL returns a flat list of field-value pairs as strings.
  3. Final Answer:

    ["age", "30", "city", "NYC"] -> Option A
  4. Quick Check:

    HGETALL returns field-value pairs list [OK]
Hint: HGETALL returns flat list of fields and values [OK]
Common Mistakes:
  • Expecting key name in output
  • Mixing data types (string vs integer)
  • Missing value for a field
4. You run the command HSET user:3 name "Bob" but get an error. Which is the most likely cause?
medium
A. Redis does not support hashes with string fields
B. The key user:3 already holds a string value
C. HSET requires a numeric field value
D. The field name is reserved and cannot be used

Solution

  1. Step 1: Understand Redis type errors

    If the key 'user:3' holds a string, trying to use HSET (hash command) causes a type error.
  2. Step 2: Check other options

    Field names are not reserved, HSET accepts strings, and Redis supports string fields in hashes.
  3. Final Answer:

    The key user:3 already holds a string value -> Option B
  4. Quick Check:

    Type mismatch causes HSET error [OK]
Hint: Check key type before using hash commands [OK]
Common Mistakes:
  • Assuming field names are restricted
  • Thinking HSET needs numeric values
  • Ignoring Redis data type rules
5. You want to store user preferences with many optional fields but minimize memory. Which Redis approach is best?
hard
A. Use a set with all field names as members
B. Use a list with all possible fields and empty strings for missing
C. Use separate string keys for each field
D. Use a hash with only fields that have values

Solution

  1. Step 1: Consider memory efficiency

    Hashes store only existing fields, saving memory by omitting empty or missing fields.
  2. Step 2: Evaluate other options

    Lists with empty strings waste memory; sets store unique members but not key-value pairs; separate keys increase overhead.
  3. Final Answer:

    Use a hash with only fields that have values -> Option D
  4. Quick Check:

    Hashes save memory by storing only present fields [OK]
Hint: Store only present fields in hashes to save memory [OK]
Common Mistakes:
  • Using lists with empty placeholders
  • Using sets for key-value data
  • Creating many separate keys
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