Bird
Raised Fist0
Redisquery~20 mins

RENAME and RENAMENX in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Rename Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the result of RENAME when the new key exists?
Assume the Redis database has keys: key1 with value 100 and key2 with value 200. What happens after running RENAME key1 key2?
Redis
SET key1 100
SET key2 200
RENAME key1 key2
GET key2
AReturns an error because key2 already exists
BDeletes both keys and returns nil
CDoes nothing and returns OK; GET key2 returns 200
DRenames key1 to key2, overwriting the old key2 value; GET key2 returns 100
Attempts:
2 left
💡 Hint
RENAME overwrites the destination key if it exists.
❓ query_result
intermediate
2:00remaining
What does RENAMENX return if the new key exists?
Given keys alpha with value foo and beta with value bar, what is the return value of RENAMENX alpha beta?
Redis
SET alpha foo
SET beta bar
RENAMENX alpha beta
A0, because beta already exists and rename did not happen
B1, because the rename succeeded
CAn error because beta exists
Dnil, because alpha does not exist
Attempts:
2 left
💡 Hint
RENAMENX only renames if the new key does not exist.
📝 Syntax
advanced
2:00remaining
Which command syntax is correct to rename a key only if the new key does not exist?
Choose the correct Redis command syntax to rename oldkey to newkey only if newkey does not exist.
ARENAMENX oldkey newkey
BRENAME oldkey newkey NX
CRENAME IF NOT EXISTS oldkey newkey
DRENAMEX oldkey newkey
Attempts:
2 left
💡 Hint
The command is RENAMENX, not RENAME with options.
🧠 Conceptual
advanced
2:00remaining
What happens if you RENAME a key to the same key name?
If you run RENAME mykey mykey in Redis, what is the result?
AReturns an error because source and destination are the same
BDeletes the key
CReturns OK and does nothing
DRenames the key to a new random key
Attempts:
2 left
💡 Hint
RENAME returns OK even if source and destination are the same.
🔧 Debug
expert
3:00remaining
Why does RENAMENX fail to rename when the new key does not exist?
You run these commands: SET a 1 DEL b RENAMENX a b But RENAMENX returns 0 and does not rename. What is the most likely cause?
Redis
SET a 1
DEL b
RENAMENX a b
AKey 'b' still exists, so RENAMENX refuses to rename
BKey 'a' does not exist at the time of RENAMENX
CRENAMENX cannot rename keys with numeric values
DRENAMENX requires both keys to exist
Attempts:
2 left
💡 Hint
Check if the source key exists before renaming.

Practice

(1/5)
1. What does the Redis command RENAME do?
easy
A. Changes the name of a key and overwrites the new key if it exists
B. Changes the name of a key only if the new key does not exist
C. Deletes a key without renaming
D. Creates a copy of a key with a new name

Solution

  1. Step 1: Understand the purpose of RENAME

    The RENAME command changes the name of an existing key to a new name.
  2. Step 2: Check behavior when new key exists

    If the new key already exists, RENAME overwrites it without error.
  3. Final Answer:

    Changes the name of a key and overwrites the new key if it exists -> Option A
  4. Quick Check:

    RENAME overwrites existing keys = B [OK]
Hint: RENAME always renames and overwrites existing keys [OK]
Common Mistakes:
  • Thinking RENAME only renames if new key is free
  • Confusing RENAME with RENAMENX
  • Assuming RENAME copies instead of renaming
2. Which of the following is the correct syntax to rename a key oldkey to newkey using RENAMENX?
easy
A. RENAMENX newkey oldkey
B. RENAME oldkey newkey
C. RENAMENX oldkey newkey
D. RENAMEX oldkey newkey

Solution

  1. Step 1: Recall RENAMENX syntax

    The correct syntax is RENAMENX oldkey newkey, where the first argument is the current key and the second is the new key.
  2. Step 2: Identify incorrect options

    A reverses arguments, B uses RENAME not RENAMENX, D has a typo in command name.
  3. Final Answer:

    RENAMENX oldkey newkey -> Option C
  4. Quick Check:

    RENAMENX oldkey newkey syntax = C [OK]
Hint: Remember: RENAMENX oldkey newkey [OK]
Common Mistakes:
  • Swapping oldkey and newkey arguments
  • Using RENAME instead of RENAMENX
  • Typo in command name
3. Given the following Redis commands executed in order:
SET key1 "value1"
SET key2 "value2"
RENAMENX key1 key2
GET key1
GET key2

What will be the output of the two GET commands?
medium
A. GET key1: "value1", GET key2: "value2"
B. GET key1: nil, GET key2: "value1"
C. GET key1: "value1", GET key2: "value1"
D. GET key1: nil, GET key2: "value2"

Solution

  1. Step 1: Analyze RENAMENX behavior

    RENAMENX renames key1 to key2 only if key2 does not exist. Here, key2 exists, so RENAMENX does nothing.
  2. Step 2: Check GET results

    Since RENAMENX failed, key1 still holds "value1" and key2 holds "value2".
  3. Final Answer:

    GET key1: "value1", GET key2: "value2" -> Option A
  4. Quick Check:

    RENAMENX fails if new key exists = A [OK]
Hint: RENAMENX fails if new key exists, keys unchanged [OK]
Common Mistakes:
  • Assuming RENAMENX overwrites existing keys
  • Expecting key1 to be deleted after RENAMENX
  • Confusing RENAMENX with RENAME
4. You run the command RENAMENX keyA keyB but get an error saying ERR no such key. What is the most likely cause?
medium
A. keyB does not exist
B. RENAMENX command is misspelled
C. keyB already exists
D. keyA does not exist

Solution

  1. Step 1: Understand error meaning

    The error ERR no such key means the source key to rename does not exist.
  2. Step 2: Identify which key is source

    In RENAMENX keyA keyB, keyA is the source key. If it does not exist, the error occurs.
  3. Final Answer:

    keyA does not exist -> Option D
  4. Quick Check:

    ERR no such key means source key missing = D [OK]
Hint: ERR no such key means source key missing [OK]
Common Mistakes:
  • Thinking error means destination key missing
  • Assuming RENAMENX overwrites keys
  • Ignoring command spelling
5. You want to rename a key session123 to user_session only if user_session does not exist, and get a confirmation if rename succeeded. Which Redis command and response should you use?
hard
A. Use RENAME session123 user_session and expect OK response
B. Use RENAMENX session123 user_session and expect 1 if renamed, 0 if not
C. Use RENAMENX user_session session123 and expect 1 if renamed
D. Use RENAME user_session session123 and expect OK response

Solution

  1. Step 1: Choose command that renames only if new key is free

    RENAMENX renames only if the new key does not exist, perfect for this case.
  2. Step 2: Understand RENAMENX response

    RENAMENX returns 1 if rename succeeded, 0 if new key exists and rename skipped.
  3. Final Answer:

    Use RENAMENX session123 user_session and expect 1 if renamed, 0 if not -> Option B
  4. Quick Check:

    RENAMENX returns 1 or 0 for success/failure = A [OK]
Hint: RENAMENX returns 1 if renamed, 0 if new key exists [OK]
Common Mistakes:
  • Using RENAME which always overwrites
  • Swapping source and destination keys
  • Expecting OK response from RENAMENX
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