Bird
Raised Fist0
Redisquery~20 mins

ZRANK and ZREVRANK for position in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sorted Set Rank Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Find the rank of a member using ZRANK
Given a sorted set with members and scores, what is the rank of member 'apple' using ZRANK?
Redis
ZADD fruits 10 apple 20 banana 15 cherry
ZRANK fruits apple
A1
B2
C0
Dnil
Attempts:
2 left
💡 Hint
ZRANK returns the zero-based rank of the member ordered from lowest to highest score.
❓ query_result
intermediate
2:00remaining
Find the reverse rank of a member using ZREVRANK
Given a sorted set with members and scores, what is the reverse rank of member 'banana' using ZREVRANK?
Redis
ZADD fruits 10 apple 20 banana 15 cherry
ZREVRANK fruits banana
A0
B1
C2
Dnil
Attempts:
2 left
💡 Hint
ZREVRANK returns the zero-based rank of the member ordered from highest to lowest score.
📝 Syntax
advanced
2:00remaining
Identify the error in using ZRANK with a non-existing member
What is the output of the command ZRANK fruits orange if 'orange' is not a member of the sorted set?
Redis
ZADD fruits 10 apple 20 banana 15 cherry
ZRANK fruits orange
A-1
B0
CError: member not found
Dnil
Attempts:
2 left
💡 Hint
ZRANK returns nil if the member does not exist in the sorted set.
❓ query_result
advanced
2:00remaining
Compare ZRANK and ZREVRANK outputs
Given the sorted set fruits with scores, what are the outputs of ZRANK fruits cherry and ZREVRANK fruits cherry?
Redis
ZADD fruits 10 apple 20 banana 15 cherry
ZRANK fruits cherry
ZREVRANK fruits cherry
AZRANK: 1, ZREVRANK: 1
BZRANK: 2, ZREVRANK: 0
CZRANK: 1, ZREVRANK: 0
DZRANK: 0, ZREVRANK: 2
Attempts:
2 left
💡 Hint
ZRANK orders from lowest to highest score, ZREVRANK from highest to lowest.
🧠 Conceptual
expert
3:00remaining
Understanding rank behavior with duplicate scores
If a sorted set has members with duplicate scores, how does ZRANK determine the rank of a member?
AZRANK returns the average rank of all members with the same score.
BZRANK orders members with the same score lexicographically and returns the zero-based rank accordingly.
CZRANK returns the highest rank among members with the same score.
DZRANK returns a random rank among members with the same score.
Attempts:
2 left
💡 Hint
Redis sorts members with the same score by their member name in alphabetical order.

Practice

(1/5)
1. What does the Redis command ZRANK return when used on a sorted set?
easy
A. The total number of members in the sorted set
B. The zero-based rank of a member with scores in ascending order
C. The score of the member in the sorted set
D. The member with the highest score

Solution

  1. Step 1: Understand what ZRANK does

    ZRANK returns the position of a member in a sorted set ordered by score from lowest to highest.
  2. Step 2: Identify the rank type

    The rank is zero-based, meaning the lowest score member has rank 0.
  3. Final Answer:

    The zero-based rank of a member with scores in ascending order -> Option B
  4. Quick Check:

    ZRANK = ascending zero-based rank [OK]
Hint: ZRANK ranks ascending, starting at zero [OK]
Common Mistakes:
  • Confusing rank with score
  • Thinking rank starts at 1
  • Mixing ZRANK with ZREVRANK
2. Which of the following is the correct syntax to get the rank of member 'user1' in sorted set 'scores' using ZREVRANK?
easy
A. ZRANK user1 scores
B. ZREVRANK user1 scores
C. ZRANK scores user1
D. ZREVRANK scores user1

Solution

  1. Step 1: Recall ZREVRANK command syntax

    The syntax is: ZREVRANK key member
  2. Step 2: Apply to given example

    Here, key is 'scores' and member is 'user1', so command is ZREVRANK scores user1
  3. Final Answer:

    ZREVRANK scores user1 -> Option D
  4. Quick Check:

    ZREVRANK key member syntax [OK]
Hint: Command syntax: ZREVRANK key member [OK]
Common Mistakes:
  • Swapping key and member order
  • Using ZRANK instead of ZREVRANK
  • Adding extra parameters
3. Given a sorted set 'players' with members and scores: {"alice": 50, "bob": 70, "carol": 60}, what is the result of ZRANK players bob?
medium
A. 2
B. 1
C. 0
D. None (member not found)

Solution

  1. Step 1: Sort members by ascending score

    Sorted by score ascending: alice(50), carol(60), bob(70)
  2. Step 2: Find zero-based rank of 'bob'

    bob is third in order, so rank is 2 (0-based)
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    ZRANK ascending order rank = 2 [OK]
Hint: ZRANK counts from lowest score, zero-based [OK]
Common Mistakes:
  • Assuming rank is score
  • Confusing ascending with descending order
  • Using ZREVRANK result instead
4. You run ZREVRANK leaderboard "playerX" but get a null reply. What is the most likely reason?
medium
A. "playerX" is not a member of the sorted set "leaderboard"
B. The command syntax is incorrect
C. The sorted set "leaderboard" is empty
D. ZREVRANK only works on lists, not sorted sets

Solution

  1. Step 1: Understand ZREVRANK null reply meaning

    Null reply means the member does not exist in the sorted set.
  2. Step 2: Check other options

    Syntax is correct, empty set would also return null but question implies member missing, and ZREVRANK works only on sorted sets.
  3. Final Answer:

    "playerX" is not a member of the sorted set "leaderboard" -> Option A
  4. Quick Check:

    Null reply means member missing [OK]
Hint: Null reply means member not in set [OK]
Common Mistakes:
  • Thinking syntax error causes null
  • Assuming empty set returns error
  • Confusing data types for ZREVRANK
5. You have a sorted set 'scores' with members and scores: {"x": 10, "y": 20, "z": 30}. You want to find the rank of member 'y' counting from highest score first. Which command and expected result is correct?
hard
A. ZRANK scores y returns 1
B. ZREVRANK scores y returns 2
C. ZREVRANK scores y returns 1
D. ZRANK scores y returns 2

Solution

  1. Step 1: Understand ZREVRANK ranks descending

    ZREVRANK ranks members from highest to lowest score, zero-based.
  2. Step 2: Sort members descending and find 'y' rank

    Descending order: z(30), y(20), x(10). 'y' is second, so rank is 1.
  3. Final Answer:

    ZREVRANK scores y returns 1 -> Option C
  4. Quick Check:

    ZREVRANK descending zero-based rank = 1 [OK]
Hint: ZREVRANK ranks from highest score, zero-based [OK]
Common Mistakes:
  • Mixing ZRANK and ZREVRANK results
  • Counting rank starting at 1 instead of 0
  • Confusing ascending and descending order
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