Identify Time Complexity

Easy~5 min

Given a string describing a code pattern, return its Big O time complexity as a string.

The possible inputs and their expected outputs are:

| Input | Output | Why | |-------|--------|-----| | "constant" | "O(1)" | Direct access, no loops | | "logarithmic" | "O(log n)" | Halving/doubling pattern | | "linear" | "O(n)" | Single loop over input | | "linearithmic" | "O(n log n)" | Sorting, then processing | | "quadratic" | "O(n^2)" | Two nested loops over input | | "exponential" | "O(2^n)" | Generating all subsets |

This problem reinforces the Big O hierarchy through pattern matching.

Examples

Example 1
Input: pattern = "linear"
Output: "O(n)"
Explanation: A single loop over the input is O(n).
Example 2
Input: pattern = "quadratic"
Output: "O(n^2)"
Explanation: Two nested loops each running n times gives n * n = O(n²).
Example 3
Input: pattern = "constant"
Output: "O(1)"
Explanation: No loops, just direct access — constant time.

Constraints

  • pattern is one of: "constant", "logarithmic", "linear", "linearithmic", "quadratic", "exponential"
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run