top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Pretty Json - InterviewBit Solution

Problem: Pretty Json


Problem Description:

Given a string A representing JSON object. Return an array of the string denoting JSON object with proper indentation.

Rules for proper indentation:

  • Every inner brace should increase one indentation to the following lines.

  • Every close brace should decrease one indentation to the same line and the following lines.

  • The indents can be increased with an additional ‘\t’

Note:

  1. [] and {} are only acceptable braces in this case.

  2. Assume for this problem that space characters can be done away with.

Input Format

The only argument given is the integer array A. 

Output Format

Return a list of strings, where each entry corresponds to a single line. The strings should not have "\n" character in them. 

For Example

Input 1:
    A = "{A:"B",C:{D:"E",F:{G:"H",I:"J"}}}"
Output 1:
    { 
        A:"B",
        C: 
        { 
            D:"E",
            F: 
            { 
                G:"H",
                I:"J"
            } 
        } 
    }

Input 2:
    A = ["foo", {"bar":["baz",null,1.0,2]}]
Output 2:
   [
        "foo", 
        {
            "bar":
            [
                "baz", 
                null, 
                1.0, 
                2
            ]
        }
    ]

Solution Approach:




Solution:

Code in C++


If you have any questions or queries, feel free to drop a comment in the comments section below.


Note: Help us improve this blog

If you have a better solution, and you think you can help your peers to understand this problem better, then please drop your solution and approach in the comments section below.

We will upload your approach and solution here by giving you the proper credit so that you can showcase it among your peers.

bottom of page