Magicsheet logo

Reverse Degree of a String

Easy
95.7%
Updated 6/1/2025

Asked by 1 Company

Reverse Degree of a String

What is this problem about?

The Reverse Degree of a String interview question asks you to compute a "degree" value for a given string. The degree is calculated by: for each character, multiply its 1-based position in the reversed alphabet (so 'z' = 1, 'y' = 2, ..., 'a' = 26) by its 1-based position in the string (1-indexed). Sum all these products and return the total as the degree of the string.

Why is this asked in interviews?

Capgemini includes this as a beginner-level string and simulation screening question. It tests basic string traversal, character arithmetic, and formula application. While straightforward, it distinguishes candidates who can translate a mathematical specification directly into clean code from those who make off-by-one errors or misread the "reversed alphabet" mapping.

Algorithmic pattern used

The pattern is linear string traversal with character arithmetic. For each character s[i] (0-indexed), compute:

  • Reversed alphabet position: ord('z') - ord(s[i]) + 1 = 123 - ord(s[i]).
  • String position (1-indexed): i + 1.
  • Contribution: (123 - ord(s[i])) * (i + 1).

Sum all contributions and return the total.

Example explanation

String: "ab"

  • 'a' at position 1: reversed alphabet position = 123 - 97 = 26. Contribution: 26 × 1 = 26.
  • 'b' at position 2: reversed alphabet position = 123 - 98 = 25. Contribution: 25 × 2 = 50.

Degree = 26 + 50 = 76.

String: "z":

  • 'z' at position 1: reversed alphabet = 123 - 122 = 1. Contribution: 1 × 1 = 1. Degree = 1.

Common mistakes candidates make

  • Using ord('a') - ord(s[i]) + 1 (forward alphabet) instead of the reversed alphabet formula.
  • Using 0-indexed positions instead of 1-indexed, producing an off-by-one error throughout.
  • Forgetting that the mapping is 'z'=1, 'y'=2, ..., 'a'=26, not 'a'=1, 'b'=2.
  • Applying the reversed position to the string index instead of the character mapping.

Interview preparation tip

For the Reverse Degree of a String coding problem, the string and simulation interview pattern is straightforward. The key formula: reversed_pos = 123 - ord(char) (where 123 = ord('z') + 1). Practice computing this for a few characters on paper to verify correctness before coding. Capgemini interviewers use this as a quick sanity check for code accuracy — solve it in under 3 minutes and double-check your formula derivation.

Similar Questions