0
0
Redisquery~20 mins

INCR and DECR for counters in Redis - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Counter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
What is the value after incrementing a counter?
Given a Redis key counter with initial value 5, what will be the value after running INCR counter once?
Redis
SET counter 5
INCR counter
GET counter
A5
B6
C7
DError: key is not an integer
Attempts:
2 left
💡 Hint
INCR adds 1 to the current integer value stored at the key.
query_result
intermediate
1:30remaining
What happens when DECR is used on a non-existing key?
If the Redis key visits does not exist, what will be the result of DECR visits?
Redis
DEL visits
DECR visits
GET visits
A-1
B0
CError: key does not exist
Dnil
Attempts:
2 left
💡 Hint
INCR and DECR treat missing keys as zero before operation.
📝 Syntax
advanced
2:00remaining
Which command sequence correctly increments a counter only if it exists?
You want to increment the Redis key score only if it already exists. Which sequence achieves this without creating the key if missing?
A
EXISTS score
INCR score
BINCRBY score 1
CINCR score
D
WATCH score
INCR score
EXEC
Attempts:
2 left
💡 Hint
Check if the key exists before incrementing.
query_result
advanced
1:30remaining
What is the final value after multiple increments and decrements?
Starting with counter set to 10, what is the value after these commands? INCR counter DECR counter DECR counter INCR counter
Redis
SET counter 10
INCR counter
DECR counter
DECR counter
INCR counter
GET counter
A11
B9
C10
D8
Attempts:
2 left
💡 Hint
Count how many increments and decrements happen.
🧠 Conceptual
expert
2:00remaining
Why might using INCR on a key storing a non-integer value cause an error?
Consider a Redis key user:count storing the string value hello. What happens if you run INCR user:count and why?
Redis
SET user:count hello
INCR user:count
ARedis converts 'hello' to 0 and increments to 1
BThe value becomes 'hello1' by appending 1
CThe value resets to 1 ignoring the old value
DRedis returns an error because the value is not an integer
Attempts:
2 left
💡 Hint
INCR expects the key to hold an integer value or be missing.