Samsung Interview Question for Nones


Country: India
Interview Type: Written Test




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

#include "stdafx.h"
#include "stdio.h"

static int bar_data[50];
static int max_length;
static int num_of_bars;
static int total_length;

int FindSum(int length, char* IsUsed)
{
	int i;

	printf("\n FindSum: [%d] ", length);

	for (i = 0; i < num_of_bars; i++)
	{
		printf("%d ", IsUsed[i]);
	}
	printf("\n");

#if 0
	for (i = num_of_bars - 1; i >= 0; i--)
	{
		if ((IsUsed[i] == 0) && ((length - bar_data[i]) == 0))
		{
			IsUsed[i] = 1;
			return 1;
		}
	}
#endif

	for (i = num_of_bars - 1; i >= 0; i--)
	{
		if ((IsUsed[i] == 0) && ((length - bar_data[i]) >= 0))
		{
			IsUsed[i] = 1;
			length -= bar_data[i];

			if (length == 0)
			{
				return 1;
			}

			if (FindSum(length, IsUsed) == 0)
			{
				IsUsed[i] = 0;
			}
			else
			{
				return 1;
			}
		}
	}
	return 0;
}

int CalculateHeight()
{
	int i = 0, j = 0, h1 = 0, h2 = 0;

	int max_height = 0;

	char IsUsed[51];

	for (i = num_of_bars - 1; i > 0; i--)
	{
		if (bar_data[i] > total_length)
		{
			j++;
			total_length -= bar_data[i];
		}
	}
	max_height = total_length / 2;
	num_of_bars -= j;

	j = 0;

	for (i = max_height; i > 1; i--)
	{
		memset(IsUsed, '\0', sizeof(IsUsed));

		h1 = FindSum(i, IsUsed);
		if (h1)
			h2 = FindSum(i, IsUsed);

		if (h1 & h2)
		{
			j = i;
			break;
		}
	}

	return j;
}


int main()
{
	int i;

	printf("\n Enter Number of Bars: ");
	scanf_s("%d", &num_of_bars);

	printf("\n Enter Bar Code Length's: ");
	total_length = 0;

	for (i = 0; i < num_of_bars; i++)
	{
		scanf_s("%d", &bar_data[i]);
		total_length += bar_data[i];
	}

	printf("\n Max Height: %d", CalculateHeight());

	getchar();

	return 0;
}

- NoName December 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(S^2 * N) time, where S is sum of the input pipes' lengths, and N is number of the pipes.

#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

void LargestPipes(vector<int> const &a, vector<int> &p1, vector<int> &p2, vector<int> &p1_out,
	vector<int> &p2_out, int16_t &len_out, unordered_set<uint64_t> &memo,
	int16_t len1 = 0, int16_t len2 = 0, int idx = 0)
{
	if (idx < 0 ||
		idx > a.size())
	{
		return;
	}
	if (len1 == len2 &&
		len1 > len_out)
	{
		len_out = len1;
		p1_out = p1;
		p2_out = p2;
	}
	if (idx == a.size()) {
		return;
	}

	uint64_t memo_key = (static_cast<uint64_t>(len1) << 48) | (static_cast<uint64_t>(len2) << 32) | idx;
	if (memo.find(memo_key) != memo.end()) {
		return;
	}
	memo.insert(memo_key);

	p1.push_back(a[idx]);
	LargestPipes(a, p1, p2, p1_out, p2_out, len_out, memo, len1 + a[idx], len2, idx + 1);
	p1.pop_back();

	p2.push_back(a[idx]);
	LargestPipes(a, p1, p2, p1_out, p2_out, len_out, memo, len1, len2 + a[idx], idx + 1);
	p2.pop_back();

	LargestPipes(a, p1, p2, p1_out, p2_out, len_out, memo, len1, len2, idx + 1);
}

int main()
{
	vector<int> p1, p2, p1_out, p2_out;
	int16_t len_out;
	unordered_set<uint64_t> memo;
	LargestPipes({1, 2, 3, 4, 6}, p1, p2, p1_out, p2_out, len_out, memo);

	for (int n : p1_out) {
		cout << n << ", ";
	}
	cout << "\n";
	for (int n : p2_out) {
		cout << n << ", ";
	}
	cout << "\n";
}

- Alex December 13, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you please tell the idea behind this code?
i don't understand the question properly.

- vmkaabhirami January 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@vmkaabhirami. I'm also not sure if I understand the question correctly. I assume that out of the set of pipes I should build two pipes (by connecting them) as long as possible. Also, I assume that the lengths of the two long pipes should be equal.

- Alex January 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Alex can you explain the logic you used, I am unable to get it

- Sudheer January 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

is this samsung's professional or above level test question

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

This question had been asked recently in samsung r&d test. ! NoName solution will not pass all test case(s).

- Himanshu Singh March 09, 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