Explorer
JavaScript

Write a function `myStringify(value)` that converts a value to a JSON string. Handle strings, numbers, booleans, null, arrays, and objects.

Problem Statement

<p>Write a function <code>myStringify(value)</code> that converts a value to a JSON string. Handle strings, numbers, booleans, null, arrays, and objects.</p>

Examples

Input: myStringify({ a: 1, b: "hello", c: [true, null] })

Output: '{"a":1,"b":"hello","c":[true,null]}'

Explanation: Serialized to valid JSON string.

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Handle each type separately: null, boolean, number, string, array, object. šŸ’” Hint 2: Strings must be wrapped in double quotes. Recursively stringify arrays and objects. šŸ’” Hint 3: For arrays: "[" + elements.map(myStringify).join(",") + "]". For objects: use Object.entries.

Editorial & Approach

Problem Overview & Intuition

To solve Implement JSON.stringify (Simplified), we consider the execution characteristics of JavaScript engines. Write a function `myStringify(value)` that converts a value to a JSON string. Handle strings, numbers, booleans, null, arrays, and objects. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (handle each type separately: null, boolean, number, string, array, object).
  3. Implement Logic: Handle state and transformations efficiently (strings must be wrapped in double quotes. recursively stringify arrays and objects).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function myStringify(value) {
  if (value === null) return "null";
  if (typeof value === "boolean") return String(value);
  if (typeof value === "number") return String(value);
  if (typeof value === "string") return '"' + value + '"';
  if (Array.isArray(value)) return "[" + value.map(myStringify).join(",") + "]";
  if (typeof value === "object") {
    const entries = Object.entries(value)
      .filter(([,v]) => v !== undefined)
      .map(([k,v]) => '"' + k + '":'  + myStringify(v));
    return "{" + entries.join(",") + "}";
  }
  return undefined;
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

Implement JSON.stringify (Simplified)

Hard

Write a function myStringify(value) that converts a value to a JSON string. Handle strings, numbers, booleans, null, arrays, and objects.

Example Scenarios
1Example 1
Input: myStringify({ a: 1, b: "hello", c: [true, null] })
Output: '{"a":1,"b":"hello","c":[true,null]}'
Explanation:

Serialized to valid JSON string.

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
["42","\"hello\"","true","null","[1,\"a\",null]","{\"a\":1,\"b\":\"two\"}"]