Samsung Interview Question for Developer Program Engineers


Country: India
Interview Type: Written Test




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

Question is not clear. Can you please rephrase the question and edit the question. When n > 0, doesn't it imply that it can be greater than 4?? One more doubt I have is given example for n=5
1,11,111,1111,11111,12341 , the numbers 222, 2222, 333, 3333, .... are also valid right?

- prudent_programmer March 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If my understanding is correct, following is the way to solve this.
Taking a simple example of 2 numbers spread over 3 place.
array A = {1,2}; n=3
say the number is: xyz
I see that repetition is allowed, as per the example.
when n=3, we have to take into account n=2 and n=1 based on what I see from the example given in the question.
x can have 1 or 2; y also can have 1 or 2; z also can have 1 or 2.
So the possibilities of all types of 3 digit numbers is: 2*2*2 = 8
Same way, possibilities of all types of 2 digit numbers is 2*2=4
Same way possibilities of all types of 1 digit number is 2
In this case answer would be 14.

Going by this, following snippet would reap the result.

numOfPlaces = 1
while(numOfPlaces<=n) {
result = result + array.length ^ numOfPlaces;
numOfPlaces++;
}

Note: I have not tested the while loop. but conceptually, I believe I have given a fair answer based on the question.
Correct me if wrong.

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

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;

List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}

public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}

return groups;
}

private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}

for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}

Now, when you run this code with n = 5 and the array {1, 2, 3, 4}, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of n and the array arr to generate groups for different scenarios.

- Anonymous April 19, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;

List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}

public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}

return groups;
}

private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}

for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}

Now, when you run this code with n = 5 and the array {1, 2, 3, 4}, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of n and the array arr to generate groups for different scenarios.

- Vaishali Jain April 19, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayGroups {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int n = 5;

        List<String> result = generateGroups(arr, n);
        System.out.println("Groups for n = " + n + ":");
        for (int i = 0; i < result.size(); i++) {
            System.out.println((i + 1) + ". " + result.get(i));
        }
        System.out.println("Total groups: " + result.size());
    }

    public static List<String> generateGroups(int[] arr, int n) {
        List<String> groups = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) {
            generateGroupsUtil(arr, 0, i, sb, groups);
        }

        return groups;
    }

    private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
        if (length == 0) {
            groups.add(sb.toString());
            return;
        }

        for (int i = index; i < arr.length; i++) {
            sb.append(arr[i]);
            generateGroupsUtil(arr, i, length - 1, sb, groups);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}


Now, when you run this code with `n = 5` and the array `{1, 2, 3, 4}`, it will produce the output with the count of arrays along with the arrays themselves. Similarly, you can change the value of `n` and the array `arr` to generate groups for different scenarios.

- Vaishali Jain April 19, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayGroups {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4};
int n = 5;

List<String> result = generateGroups(arr, n);
System.out.println("Groups for n = " + n + ":");
for (int i = 0; i < result.size(); i++) {
System.out.println((i + 1) + ". " + result.get(i));
}
System.out.println("Total groups: " + result.size());
}

public static List<String> generateGroups(int[] arr, int n) {
List<String> groups = new ArrayList<>();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < n; i++) {
generateGroupsUtil(arr, 0, i, sb, groups);
}

return groups;
}

private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
if (length == 0) {
groups.add(sb.toString());
return;
}

for (int i = index; i < arr.length; i++) {
sb.append(arr[i]);
generateGroupsUtil(arr, i, length - 1, sb, groups);
sb.deleteCharAt(sb.length() - 1);
}
}
}

- Vaishali Jain April 19, 2024 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;

public class IntegerArrayGroups {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4};
        int n = 5;

        List<String> result = generateGroups(arr, n);
        System.out.println("Groups for n = " + n + ":");
        for (int i = 0; i < result.size(); i++) {
            System.out.println((i + 1) + ". " + result.get(i));
        }
        System.out.println("Total groups: " + result.size());
    }

    public static List<String> generateGroups(int[] arr, int n) {
        List<String> groups = new ArrayList<>();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) {
            generateGroupsUtil(arr, 0, i, sb, groups);
        }

        return groups;
    }

    private static void generateGroupsUtil(int[] arr, int index, int length, StringBuilder sb, List<String> groups) {
        if (length == 0) {
            groups.add(sb.toString());
            return;
        }

        for (int i = index; i < arr.length; i++) {
            sb.append(arr[i]);
            generateGroupsUtil(arr, i, length - 1, sb, groups);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}

- Vaishali Jain April 19, 2024 | 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