Bird
Raised Fist0
Redisquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Redis command INCR mycounter do if mycounter does not exist yet?
easy
A. It creates mycounter with value 1.
B. It returns an error because the key does not exist.
C. It creates mycounter with value 0.
D. It sets mycounter to -1.

Solution

  1. Step 1: Understand INCR behavior on missing keys

    When INCR is called on a key that does not exist, Redis treats it as 0 before incrementing.
  2. Step 2: Apply increment to initial value

    Incrementing 0 by 1 results in 1, so Redis sets mycounter to 1.
  3. Final Answer:

    It creates mycounter with value 1. -> Option A
  4. Quick Check:

    INCR on missing key = 1 [OK]
Hint: INCR on missing key starts at 1 automatically [OK]
Common Mistakes:
  • Thinking INCR returns error if key missing
  • Assuming key starts at 0 after INCR
  • Confusing INCR with setting key manually
2. Which of the following is the correct syntax to decrease a Redis key named pageviews by 1?
easy
A. DECREMENT pageviews
B. DECR pageviews
C. INCR pageviews -1
D. DECR pageviews 1

Solution

  1. Step 1: Recall Redis command for decrement by 1

    The correct Redis command to decrease a key by 1 is DECR key.
  2. Step 2: Check options for correct syntax

    DECR pageviews uses DECR pageviews, which is valid. DECREMENT pageviews is invalid command, C is incorrect syntax, and D is invalid because DECR does not accept an amount argument.
  3. Final Answer:

    DECR pageviews -> Option B
  4. Quick Check:

    Use DECR key to subtract 1 [OK]
Hint: Use DECR key to subtract 1 exactly [OK]
Common Mistakes:
  • Using DECREMENT instead of DECR
  • Trying INCR with negative number
  • Confusing DECR and DECRBY syntax
3. Given the following Redis commands executed in order:
INCR visits
INCR visits
DECR visits
INCR visits

What is the final value of the key visits?
medium
A. 4
B. 3
C. 1
D. 2

Solution

  1. Step 1: Track each command's effect on visits

    Start: visits does not exist, treated as 0.
    INCR visits -> 1
    INCR visits -> 2
    DECR visits -> 1
    INCR visits -> 2
  2. Step 2: Recalculate carefully

    Re-check: Actually, after DECR visits from 2, visits = 1.
    Then INCR visits from 1, visits = 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    INCR twice, DECR once, INCR once = 2 [OK]
Hint: Count increments and decrements stepwise [OK]
Common Mistakes:
  • Adding all INCRs without subtracting DECR
  • Assuming visits starts at 1
  • Mixing order of commands
4. You run the command DECR mycount on a Redis key mycount that currently holds the string value "hello". What will happen?
medium
A. Redis will convert "hello" to 0 and then decrement to -1.
B. The value will become "hello-1".
C. Redis will return an error because the value is not an integer.
D. The value will remain "hello" unchanged.

Solution

  1. Step 1: Understand DECR command requirements

    DECR expects the key's value to be an integer or missing (treated as 0). A non-integer string causes an error.
  2. Step 2: Apply to given value

    Since mycount holds "hello", which is not an integer, Redis returns an error.
  3. Final Answer:

    Redis will return an error because the value is not an integer. -> Option C
  4. Quick Check:

    DECR on non-integer string = error [OK]
Hint: DECR only works on integer values [OK]
Common Mistakes:
  • Assuming Redis converts strings to 0 automatically
  • Expecting string concatenation
  • Thinking DECR silently fails
5. You want to track the number of active users on a website using Redis. Which sequence of commands correctly increases the count when a user logs in and decreases it when a user logs out, ensuring the count never goes below zero?
hard
A. Use INCR active_users on login and before DECR active_users on logout, check if value > 0.
B. Use INCRBY active_users 1 on login and DECRBY active_users 1 on logout without checks.
C. Use INCR active_users on login and DECR active_users on logout without checks.
D. Use INCR active_users on login and DEL active_users on logout.

Solution

  1. Step 1: Understand counting active users

    Increment on login is simple with INCR. Decrement on logout must avoid negative counts.
  2. Step 2: Prevent negative counts on logout

    Before calling DECR, check if active_users > 0 to avoid going below zero.
  3. Final Answer:

    Use INCR on login and check value > 0 before DECR on logout. -> Option A
  4. Quick Check:

    Check before DECR to avoid negatives [OK]
Hint: Check count > 0 before DECR to prevent negatives [OK]
Common Mistakes:
  • Not checking before DECR causes negative counts
  • Deleting key on logout resets count incorrectly
  • Assuming DECRBY and DECR behave differently here
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