iLabs Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
same as question?id=14809839
follow up question: output the numbers which constitutes the max sum
The idea is right, although the code will not run as written. I like the solution because it is so simple. Here are a few small corrections:
public static void main(String[] args)
{
// TODO code application logic here
int[] array = {-10, -3, -2, -9, -1, -8};
int startIndex = 0;
int startSum = 0;
int maxSum = maxSum(array, startIndex, startSum);
if(maxSum == 0)
{
maxSum = maxElementInArray(array);
}
System.out.println(maxSum);
}
public static int maxElementInArray(int[] A)
{
int max = Integer.MIN_VALUE;
for (int index = 0; index < A.length; index++)
{
if (A[index] > max)
{
max = A[index];
}
}
return max;
}
public static int max(int a, int b)
{
if (a > b)
{
return a;
}
else return b;
}
public static int maxSum(int[] A , int currIndex , int sum)
{
if(currIndex >= A.length)
{
return 0;
}
return sum+max(maxSum(A,currIndex+1,sum),A[currIndex]+maxSum(A,currIndex+2,sum));
}
Considering 'max sum' means subarray with maximum sum and also that this subarray is composed of alternate numbers(since two numbers should not be adjacent to each other) here is the solution I have in mind.
Split the array into two.
First array has all elements at even index, 2nd array has elements in odd index.
Then apply largest contiguous subarray sum method to both.
Return the higher one as the result
Keep two variables to store the 'max till the boundary' and 'max before the boundary'. Iterate over the elements of the array one by one and keep updating the variables. This is similar to the O(n) maximum subarray problem.
static int getMaxSubSeqNonAdjacent(int[] array) {
int sumTillBoundary = array[1];
int sumBeforeBoundary = array[0];
for (int i = 2; i < array.length; i++){
int sumBeforeBoundary2 = sumBeforeBoundary;
sumBeforeBoundary = Math.max(sumTillBoundary, sumBeforeBoundary);
sumTillBoundary = Math.max(sumBeforeBoundary2 + array[i], array[i]);
}
return Math.max(sumTillBoundary, sumBeforeBoundary);
}
C++11:
// TASK: write a insert method for singly linked list, so that it will insert elements in the ascending order.
template <typename T>
class Node {
public:
using Type = T;
template <typename... Args>
Node(Args&&... args) : value_(args...) {}
void setNext(Node *next) {
this->next_ = next;
}
const Node * next() const {
return next_;
}
Node * next() {
return next_;
}
void setValue(const Type &value) {
this->value_ = value;
}
const Type & value() const {
return value_;
}
Type & value() {
return value_;
}
private:
enum class ConstructorType { Clone };
Node(const Node &other, ConstructorType) {
this->next_ = other.next_;
this->value_ = other.value_;
}
public:
Node * clone() const {
return new Node(*this, ConstructorType::Clone);
}
private:
Node *next_ = nullptr;
Type value_;
};
template <typename T>
class List {
public:
using Type = T;
using Node = ::Node<T>;
const Node * operator [](unsigned long index) const {
Node *ptr = first_;
while (ptr->next() && index > 0) {
ptr = ptr->next();
index--;
}
return ptr;
}
List & operator <<(Node node) {
// return this->operator <<(node);
//}
//
//List & operator <<(Node &node) {
if (!first_) {
first_ = node.clone();
} else {
Node *ptr = first_;
while (ptr->next() && ptr->next()->value() < node.value()) {
ptr = ptr->next();
}
if (ptr != first_) {
ptr->setNext(node.clone());
ptr->next()->setNext(ptr);
} else {
ptr = node.clone();
ptr->setNext(first_);
first_ = ptr;
}
return *this;
}
}
Node * first() {
return this->first_;
}
private:
Node *first_ = nullptr;
};
#include <assert.h>
int main() {
// TEST: basic usage of list
{
List<int> list;
list << 3 << 2 << 1;
Node<int> *ptr = list.first();
assert(ptr->value() == 1);
ptr = ptr->next();
assert(ptr->value() == 2);
ptr = ptr->next();
assert(ptr->value() == 3);
assert(ptr->next() == nullptr);
}
// TEST: operator []
{
List<int> list;
list << 3 << 2 << 1;
assert(list[0]->value() == 1);
assert(list[1]->value() == 2);
assert(list[2]->value() == 3);
}
}
This should work..
- Anonymous November 11, 2012