Bird
Raised Fist0
Redisquery~20 mins

Accessing keys and arguments in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Keys and Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Accessing KEYS and ARGV in a Lua script
Given the following Redis Lua script, what will be the output if KEYS = ['user:1', 'user:2'] and ARGV = ['100', '200']?

return {KEYS[1], ARGV[2]}
Redis
return {KEYS[1], ARGV[2]}
A["user:1", "200"]
B["user:2", "100"]
C["user:1", "100"]
D["user:2", "200"]
Attempts:
2 left
💡 Hint
Remember that KEYS and ARGV are 1-based indexed in Redis Lua scripts.
🧠 Conceptual
intermediate
2:00remaining
Understanding KEYS and ARGV usage in Redis scripts
Why does Redis separate input parameters into KEYS and ARGV in Lua scripts?
ATo allow passing only keys without any arguments.
BTo optimize memory usage by separating keys and arguments.
CTo distinguish keys that will be accessed for locking and replication from other arguments.
DTo enforce that keys must be strings and arguments must be numbers.
Attempts:
2 left
💡 Hint
Think about Redis cluster and command atomicity.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in accessing KEYS and ARGV
Which option contains a syntax error when trying to access the first key and first argument in a Redis Lua script?
Redis
return {KEYS[0], ARGV[1]}
Areturn {KEYS[1], ARGV[1]}
Breturn {KEYS[0], ARGV[1]}
Creturn {KEYS[1], ARGV[0]}
Dreturn {KEYS[1], ARGV[2]}
Attempts:
2 left
💡 Hint
Lua arrays start at 1, not 0.
🔧 Debug
advanced
2:00remaining
Debugging argument access in Redis Lua script
A Redis Lua script returns nil when trying to access ARGV[3], but ARGV has 2 elements. What is the cause?
AARGV is empty because KEYS were passed instead.
BARGV indexing starts at 0, so ARGV[3] is invalid.
CARGV must be accessed with string keys, not numeric indices.
DARGV[3] is out of range because only 2 arguments were passed.
Attempts:
2 left
💡 Hint
Check how many arguments are passed in ARGV.
❓ optimization
expert
2:00remaining
Optimizing Redis Lua script argument usage
You have a Redis Lua script that receives 10 keys and 10 arguments. You only need to access the first 3 keys and first 5 arguments. Which approach is best for performance?
APass only the needed keys and arguments to the script to reduce input size.
BLoop over all 10 KEYS and 10 ARGV and ignore unused ones.
CCopy KEYS and ARGV into new tables with only needed elements before use.
DAccess only KEYS[1] to KEYS[3] and ARGV[1] to ARGV[5] directly without looping over all.
Attempts:
2 left
💡 Hint
Minimize data passed to the script for best performance.

Practice

(1/5)
1. In a Redis Lua script, which array is used to access the keys passed to the script?
easy
A. KEYS
B. ARGV
C. KEYSARGS
D. ARGS

Solution

  1. Step 1: Understand Redis script inputs

    Redis Lua scripts receive keys and arguments separately.
  2. Step 2: Identify the array for keys

    The keys are accessed using the KEYS array inside the script.
  3. Final Answer:

    KEYS -> Option A
  4. Quick Check:

    Keys = KEYS [OK]
Hint: Keys are always in KEYS array, arguments in ARGV [OK]
Common Mistakes:
  • Confusing KEYS with ARGV
  • Using ARGS instead of ARGV
  • Assuming keys are in ARGV
2. Which of the following is the correct way to access the first argument (not key) inside a Redis Lua script?
easy
A. KEYS[1]
B. ARGV[0]
C. ARGV[1]
D. ARGS[1]

Solution

  1. Step 1: Recall Lua array indexing

    Lua arrays start at 1, not 0.
  2. Step 2: Identify argument array

    Arguments are accessed via ARGV array, so first argument is ARGV[1].
  3. Final Answer:

    ARGV[1] -> Option C
  4. Quick Check:

    First argument = ARGV[1] [OK]
Hint: Lua arrays start at 1, so first argument is ARGV[1] [OK]
Common Mistakes:
  • Using zero-based index ARGV[0]
  • Confusing KEYS with ARGV
  • Using ARGS instead of ARGV
3. Consider this Redis Lua script snippet:
local key1 = KEYS[1]
local arg1 = ARGV[1]
return key1 .. '-' .. arg1
If the script is called with keys = ["user:1"] and arguments = ["active"], what is the returned result?
medium
A. "user:1-active"
B. "active-user:1"
C. "user:1active"
D. An error occurs

Solution

  1. Step 1: Assign values from KEYS and ARGV

    key1 = "user:1", arg1 = "active" from the inputs.
  2. Step 2: Concatenate with separator

    Return value is key1 .. '-' .. arg1 = "user:1-active".
  3. Final Answer:

    "user:1-active" -> Option A
  4. Quick Check:

    Concatenate KEYS[1] and ARGV[1] with '-' = "user:1-active" [OK]
Hint: Concatenate KEYS and ARGV with .. operator in Lua [OK]
Common Mistakes:
  • Swapping KEYS and ARGV values
  • Missing separator '-' in concatenation
  • Using zero-based indexing
4. You wrote this Redis Lua script:
local key = KEYS[1]
local arg = ARGV[0]
return key .. arg
When running the script with one key and one argument, what will happen?
medium
A. It throws an error due to invalid index
B. It returns nil concatenated with key
C. It returns only the key
D. It returns concatenated key and argument

Solution

  1. Step 1: Check Lua indexing rules

    Lua arrays start at 1, so ARGV[0] is invalid but returns nil.
  2. Step 2: Understand behavior on invalid index

    Accessing ARGV[0] returns nil, concatenating key .. nil results in key .. "nil" or error depending on Lua version.
  3. Final Answer:

    It returns nil concatenated with key -> Option B
  4. Quick Check:

    Lua arrays start at 1, ARGV[0] returns nil, concatenation with nil results in string with 'nil' [OK]
Hint: Lua arrays start at 1, never use index 0 [OK]
Common Mistakes:
  • Using zero-based index ARGV[0]
  • Assuming ARGV[0] returns first argument
  • Ignoring Lua indexing rules
5. You want to write a Redis Lua script that takes two keys and one argument. The script should return the value of the first key concatenated with the argument, then the value of the second key. Which is the correct way to access these inside the script?
hard
A. ARGV[1], KEYS[1], KEYS[2]
B. ARGV[0], KEYS[1], ARGV[1]
C. KEYS[0], ARGV[0], KEYS[1]
D. KEYS[1], ARGV[1], KEYS[2]

Solution

  1. Step 1: Identify keys and argument positions

    Two keys are KEYS[1] and KEYS[2], one argument is ARGV[1].
  2. Step 2: Access in correct order

    Access first key as KEYS[1], argument as ARGV[1], second key as KEYS[2].
  3. Final Answer:

    KEYS[1], ARGV[1], KEYS[2] -> Option D
  4. Quick Check:

    Keys in KEYS[], args in ARGV[], Lua index starts at 1 [OK]
Hint: Keys in KEYS[], args in ARGV[], index from 1 [OK]
Common Mistakes:
  • Using zero-based indexes
  • Mixing KEYS and ARGV order
  • Using ARGV for 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