Reverse mapping in numeric enums in Typescript - Time & Space Complexity
We want to understand how the time it takes to find values in a numeric enum changes as the enum grows.
Specifically, how does looking up a name by its numeric value behave as the enum size increases?
Analyze the time complexity of the following code snippet.
enum Colors {
Red, // 0
Green, // 1
Blue // 2
}
function getColorName(value: number): string | undefined {
return Colors[value];
}
This code uses a numeric enum and looks up the name of a color by its numeric value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct property access on an object (reverse mapping) without loops.
- How many times: Exactly once per lookup, no repeated traversal.
Looking up a value in the enum is like checking a label on a box directly, no matter how many boxes there are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time to find a name stays the same even if the enum grows larger.
Time Complexity: O(1)
This means the lookup time stays constant no matter how many enum members exist.
[X] Wrong: "Looking up a name by value in a numeric enum takes longer as the enum grows because it searches through all entries."
[OK] Correct: The enum is compiled into an object with direct keys, so the lookup is a simple direct access, not a search.
Understanding how enum reverse mapping works helps you explain efficient lookups in TypeScript, showing you know how language features behave under the hood.
"What if the enum used string values instead of numbers? How would the time complexity of reverse lookup change?"
Practice
Solution
Step 1: Understand numeric enums
Numeric enums assign numbers to names and create a two-way map.Step 2: Understand reverse mapping
Reverse mapping lets you use the number to get the enum name back.Final Answer:
Convert enum numeric values back to their corresponding names -> Option CQuick Check:
Reverse mapping = number to name conversion [OK]
- Thinking reverse mapping converts names to strings
- Confusing string enums with numeric enums
- Assuming enums are immutable at runtime
2 in TypeScript?Solution
Step 1: Recall enum reverse mapping syntax
Numeric enums create a reverse map accessible by bracket notation with the number.Step 2: Identify correct syntax
Using EnumName[2] returns the name for value 2.Final Answer:
EnumName[2] -> Option BQuick Check:
Bracket notation with number = Enum name [OK]
- Using dot notation with a number (invalid syntax)
- Trying to call a non-existent getName method
- Using string keys instead of numeric keys
enum Color { Red, Green, Blue }What is the output of
console.log(Color[1])?Solution
Step 1: Understand enum default values
By default, Red=0, Green=1, Blue=2 in numeric enums.Step 2: Use reverse mapping
Color[1] returns the name for value 1, which is "Green".Final Answer:
"Green" -> Option AQuick Check:
Color[1] = "Green" [OK]
- Assuming enum values start at 1
- Expecting numeric output instead of string
- Confusing enum name with value
enum Status { Active = 1, Inactive, Pending }What is wrong with this code?
console.log(Status[0]);
Solution
Step 1: Check enum values
Status starts at 1, so valid values are 1, 2, 3 for Active, Inactive, Pending.Step 2: Check reverse mapping for 0
Status[0] does not exist, so it returns undefined.Final Answer:
It returns undefined because 0 is not a valid enum value -> Option AQuick Check:
Invalid enum value returns undefined [OK]
- Expecting 0 to map to first enum name
- Thinking it causes syntax or runtime error
- Assuming reverse mapping always returns a string
enum Direction { Up = 3, Down = 5, Left = 7, Right = 9 }What will
console.log(Direction[5]) output?Solution
Step 1: Identify enum values
Direction enum assigns Up=3, Down=5, Left=7, Right=9 explicitly.Step 2: Use reverse mapping
Direction[5] returns the name for value 5, which is "Down".Final Answer:
"Down" -> Option DQuick Check:
Reverse mapping works with custom numeric values [OK]
- Assuming reverse mapping only works with default values
- Expecting numeric output instead of string
- Confusing enum names with values
Ready to prove it?
Pass the test and this pattern moves unknown
