Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

hope this helps...

public class CharCount
{

    public static void main(String[] args)
    {

        System.out.print("Enter String to find count of individual char : ");
        Scanner in = new Scanner(System.in);
        String string = in.next();

        char[] c = new char[string.length()];
        int[] count = new int[string.length()];
        int end = 0;

        boolean flag;

        for (int i = 0; i < string.length(); i++)
        {
            flag = true;
            for (int j = 0; j < end; j++)
            {
                if (string.charAt(i) == c[j])
                {
                    flag = false;
                    count[j]++;
                }
            }
            if (flag)
            {
                c[end] = string.charAt(i);
                count[end] = 1;
                end++;
            }
        }
        for (int i = 0; i < end; i++)
        {
//            System.out.println("char : " + c[i] + " count : " + count[i]);
            System.out.print(""+count[i]+c[i]);
        }
    }

}

- PeyarTheriyaa May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <iostream>
#include <string>

using std::string;

int CalculateRepeats(int inputNum){
    string inString = std::to_string(inputNum);
    char curChar = ' ';											/// THIS MUST BE CHAR
    int count = 0;
    string outString = "";
    for(int i = 0; i<inString.length(); i++){
        if(inString[i]==curChar){count++;}
        else{
            if(count!=0) {outString += std::to_string(count) + curChar;}
            count = 1;
            curChar = inString[i];
        }
    }
    if(count!=0) {outString += std::to_string(count) + curChar;}
    int outNumber = atoi(outString.c_str());								/// CONVERSION STR TO INT
    return outNumber;
}

int main(){
    int testArg = 111222289;
    int testOut = CalculateRepeats(testArg);
    std::cout << testOut;
}

- DaveFL May 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Of course the example is wrong. But the way to solve it :
1. Go over each digit keeping frequency and then read the frequency and the digit
2. If the frequency itself is more than 9, recurse.
Not solving it, because the problem [2] was meant, but not said.

- NoOne May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why is example wrong?

Input: 111222289
output: 31421819

Program should be return above output for that input

- whoknows May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@whoknows, thanks a lot for downvoting my comment.
Now I know every downvote is loosing points by 5! It is a nice game!

I am thinking which sort of english language actually allows :
"three ones **three** fours" translated into "3142" .
Thus...

- NoOne May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@PeyarTheriyaa
I had a similar solution. But I am wondering if there any better time and space complexity.

Thanks for sharing your thought

- whoknows May 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's my solution

#include <iostream>
#include <string>

using std::string;

int CalculateRepeats(int inputNum){
    string inString = std::to_string(inputNum);
    char curChar = ' ';											/// THIS MUST BE CHAR
    int count = 0;
    string outString = "";
    for(int i = 0; i<inString.length(); i++){
        if(inString[i]==curChar){count++;}
        else{
            if(count!=0) {outString += std::to_string(count) + curChar;}
            count = 1;
            curChar = inString[i];
        }
    }
    if(count!=0) {outString += std::to_string(count) + curChar;}
    int outNumber = atoi(outString.c_str());								/// CONVERSION STR TO INT
    return outNumber;
}

int main(){
    int testArg = 111222289;
    int testOut = CalculateRepeats(testArg);
    std::cout << testOut;
}

- DaveFL May 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its actually n complexity... since we parse only once through the given string.. I'd say that 's about as low as one can go... since we need to parse at least once to look through the string

- PeyarTheriyaa June 01, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More