ThoughtWorks Interview Question for Applications Developers


Country: United States
Interview Type: In-Person




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

Isn't the output wrong 3+14 = 17 but as of question no two integers should prode integer in set but 17 is in set

- upendra July 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Isn't the output wrong 3+14 = 17 but as of question no two integers should prode integer in set but 17 is in set

- upendra July 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you know the answer or have any hint to solve this question?

- GK July 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you know the solution or have any hint for this question?

- GK July 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wouldn't it be easier if you just pick the last m integers from n-1,n-2...etc

like in case of n=20 m=5

[19, 18, 17, 16, 15]

- PeyarTheriyaa July 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will not work in all cases..

- Mukul July 17, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Will not always work..

- Mukul July 17, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I understand I read the question as Shouldn't produce number in set..

- PeyarTheriyaa July 17, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What about 3+11 = 14, 6+11 = 17 ??.. The question is vague and constraints are not well defined. In many cases a solution may not exist.

- Mukul July 17, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is the best solution I could come up with

1. from 2 to n-1 add one number to set

2. for each new added number find sum with existing from set

note: step two is important if you want to maximise a+b=c triplets

public static void main(String[] args) {
    ArrayList<Integer> set = new ArrayList<>();
    Scanner in = new Scanner(System.in);
    
    System.out.print("Enter limit n : ");
    int n = in.nextInt();
    System.out.print("Enter limit m : ");
    int m = in.nextInt();
    
    outer:
    for (int i = 2; i < n && set.size() < m; i++) {
        int size = set.size();
        if (set.contains(i)) continue;
        set.add(i);
        inner:
        for (int j = size; j < set.size(); j++) {
            for (int k = 0; k < j; k++) {
                int sum = set.get(j) + set.get(k);
                if (sum >= n) continue inner;
                if (set.contains(sum)) continue;
                if (set.size() < m)
                    set.add(sum);
                else break outer;
            }
        }
    }
    
    System.out.println("the set is " + set.toString());
}

the check num>=n checks if the num has exceeded the n limit

to ensure a num is added only once we check for set contains num

- PeyarTheriyaa July 18, 2019 | 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