Bird
Raised Fist0
Redisquery~20 mins

SET and GET commands in Redis - Practice Problems & Coding Challenges

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

SET user:1 "Alice"
GET user:1

What will the GET command return?
Redis
SET user:1 "Alice"
GET user:1
A"Alice"
Buser:1
Cnil
DError: key not found
Attempts:
2 left
💡 Hint
The SET command stores a value for a key. GET retrieves that value.
❓ query_result
intermediate
1:00remaining
What happens when you GET a non-existing key?
If you run the command GET missing_key on Redis where no such key exists, what is the output?
Redis
GET missing_key
A(nil)
B"missing_key"
CError: key not found
D""
Attempts:
2 left
💡 Hint
Redis returns a special value when keys are missing.
📝 Syntax
advanced
1:30remaining
Which command syntax correctly sets a key with expiration of 10 seconds?
You want to set key "session" with value "xyz" that expires after 10 seconds. Which command is correct?
ASET session xyz PX 10
BSET session xyz TTL 10
CSET session xyz EXPIRE 10
DSET session xyz EX 10
Attempts:
2 left
💡 Hint
EX sets expiration in seconds, PX in milliseconds.
❓ query_result
advanced
1:30remaining
What is the output after overwriting a key?
Given these commands:

SET color "red"
SET color "blue"
GET color

What does the GET command return?
Redis
SET color "red"
SET color "blue"
GET color
Anil
B"red"
C"blue"
DError: key already exists
Attempts:
2 left
💡 Hint
SET overwrites existing keys with new values.
🧠 Conceptual
expert
2:00remaining
Why does GET return nil after key expiration?
You set a key with expiration using SET key value EX 5. After 6 seconds, you run GET key. Why does it return nil?
AGET command cannot retrieve keys with expiration set.
BThe key automatically deletes after expiration time, so GET finds no key.
CRedis caches old values and returns nil if cache expires.
DThe key value is hidden but still exists, GET returns nil by design.
Attempts:
2 left
💡 Hint
Expiration removes the key from the database.

Practice

(1/5)
1. What does the Redis SET command do?
easy
A. Retrieves a value by key
B. Stores a value with a specified key
C. Deletes a key and its value
D. Lists all keys in the database

Solution

  1. Step 1: Understand the purpose of SET

    The SET command in Redis is used to save or store a value associated with a key.
  2. Step 2: Compare with other commands

    GET retrieves values, DEL deletes keys, and KEYS lists keys, so they are not correct here.
  3. Final Answer:

    Stores a value with a specified key -> Option B
  4. Quick Check:

    SET saves data = A [OK]
Hint: SET saves data by key, GET fetches it [OK]
Common Mistakes:
  • Confusing SET with GET
  • Thinking SET deletes data
  • Mixing SET with key listing
2. Which of the following is the correct syntax to store the value "hello" with the key "greeting" in Redis?
easy
A. SET greeting hello
B. GET greeting hello
C. SET "greeting" "hello"
D. SET greeting, hello

Solution

  1. Step 1: Recall Redis SET syntax

    The correct syntax is SET key value without commas or quotes unless the shell requires them.
  2. Step 2: Analyze options

    SET greeting hello matches the correct syntax. GET greeting hello uses GET which is wrong. SET "greeting" "hello" uses quotes unnecessarily. SET greeting, hello uses a comma which is invalid.
  3. Final Answer:

    SET greeting hello -> Option A
  4. Quick Check:

    SET key value no commas [OK]
Hint: Use SET key value without commas or quotes [OK]
Common Mistakes:
  • Using GET instead of SET
  • Adding commas between arguments
  • Unnecessary quotes around key/value
3. Given the following Redis commands executed in order:
SET color blue
GET color
SET color red
GET color

What will be the output of the two GET commands?
medium
A. "blue" and then "red"
B. "red" and then "blue"
C. "blue" and then "blue"
D. nil and then "red"

Solution

  1. Step 1: Analyze the first SET and GET

    The first SET color blue stores "blue". The first GET color returns "blue".
  2. Step 2: Analyze the second SET and GET

    The second SET color red overwrites the value with "red". The second GET color returns "red".
  3. Final Answer:

    "blue" and then "red" -> Option A
  4. Quick Check:

    SET overwrites value, GET returns current value [OK]
Hint: SET overwrites; GET returns latest value [OK]
Common Mistakes:
  • Assuming GET returns old value
  • Thinking SET appends values
  • Expecting nil on first GET
4. You run the command GET username but get a nil response. Which of the following could fix this issue?
medium
A. Run GET username again immediately
B. Use DEL username before GET
C. Restart the Redis server
D. Run SET username "guest" before GET

Solution

  1. Step 1: Understand nil response from GET

    Nil means the key "username" does not exist or has no value stored.
  2. Step 2: Fix by setting a value

    To get a value, you must first store it using SET username "guest". Other options do not create the key.
  3. Final Answer:

    Run SET username "guest" before GET -> Option D
  4. Quick Check:

    Nil means no key; SET creates it [OK]
Hint: Nil means missing key; SET it first [OK]
Common Mistakes:
  • Retrying GET without SET
  • Deleting key before GET
  • Restarting server unnecessarily
5. You want to store a user's session token in Redis with key "session:123" and value "abc123token". Which sequence of commands correctly stores and retrieves this token?
hard
A. SET session:123 abc123token DEL session:123 GET session:123
B. GET session:123 SET session:123 abc123token
C. SET session:123 abc123token GET session:123
D. SET "session:123" "abc123token" DEL session:123 GET session:123

Solution

  1. Step 1: Store the session token

    Use SET session:123 abc123token to save the token under the key.
  2. Step 2: Retrieve the stored token

    Use GET session:123 to fetch the token value.
  3. Step 3: Analyze other options

    GET session:123 SET session:123 abc123token tries to GET before SET, so returns nil. Options A and C delete the key before GET, so GET returns nil.
  4. Final Answer:

    SET session:123 abc123token GET session:123 -> Option C
  5. Quick Check:

    SET then GET returns stored value [OK]
Hint: Always SET before GET to retrieve stored data [OK]
Common Mistakes:
  • GET before SET returns nil
  • Deleting key before GET
  • Using quotes unnecessarily
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