koustav.adorable
BAN USER
- 0of 0 votes
AnswersGiven a nested list of integers, return the sum of all integers in the list weighted by their depth.
- koustav.adorable in United States
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Different from the question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.
Example 1:
Input: [[1,1],2,[1,1]]
Output: 8
Explanation: Four 1's at depth 1, one 2 at depth 2.
Example 2:
Input: [1,[4,[6]]]
Output: 17
Explanation: One 1 at depth 3, one 4 at depth 2, and one 6 at depth 1; 13 + 42 + 6*1 = 17.| Report Duplicate | Flag | PURGE
SDE-2 Algorithm - 0of 0 votes
AnswersIn my organisation, I have m different databases, n different applications, p different resources.
- koustav.adorable in United States
How do I go about building access control system for each of this database/application on an individual user basis?
The user can raise a request for access to a resource.
The request needs to be a series of stakeholders which should be configurable. Once the user has all approvals, he/she can access the resource.| Report Duplicate | Flag | PURGE
System Design - 0of 0 votes
AnswersWhat does XMPP offer over HTTP that makes it suitable for messaging application?
- koustav.adorable in United States| Report Duplicate | Flag | PURGE
Brain Storming - 0of 2 votes
AnswersWhat is the best way to generate first N primes? (not primes up to N but first N primes)
- koustav.adorable in United States for Amazon Prime| Report Duplicate | Flag | PURGE
Amazon SDE-2 Algorithm - 0of 2 votes
AnswersDesign Amazon prime video
- koustav.adorable in Dublin for AWS| Report Duplicate | Flag | PURGE
Amazon SDE-2 System Design - -1of 1 vote
AnswersHow to design a Debugger both HLD and LLD?
- koustav.adorable in India| Report Duplicate | Flag | PURGE
Amazon SDE-3 System Design - -1of 1 vote
AnswersIn a Kafka configuration, the same message is getting replayed to the consumer again n again.This is happening under heavy load otherwise it's fine.What can be possible causes?
- koustav.adorable in United States| Report Duplicate | Flag | PURGE
Spotify SDE-2 Distributed Computing - -1of 1 vote
AnswerHow do I design a Payment Gateway system? What are the things to consider in this design ?
- koustav.adorable in United States| Report Duplicate | Flag | PURGE
Facebook SDE-2 System Design - -1of 1 vote
AnswersGiven a matrix. Convert it into a linked list matrix such that each node is connected to its next right and down node.
Ex:
1 2 3
4 5 6
7 8 9
Output:
1->2->3->NULL
| | |
v v v
4->5->6->NULL
| | |
v v v
7->8->9->NULL
| | |
v v v
--NULL-
This is my code.class Ideone { public static void main(String args[]) throws Exception { int arr[][] = { { 1, 2, 3 }, { 4, 5, 6 } }; LList op = convert2DArrintoList(arr, 0, 0); System.out.println(op); } public static LList convert2DArrintoList(int arr[][], int col, int row) { if (col >= arr[0].length || row >= arr.length) return null; return new LList(arr[row][col], convert2DArrintoList(arr, col, row + 1), convert2DArrintoList(arr, col + 1, row)); } static class LList { LList(int data) { this.data = data; } LList(int data, LList down, LList next) { this.data = data; this.down = down; this.next = next; } LList() { this.data = Integer.MAX_VALUE; } @Override public String toString() { return " " + this.data + " "; } int data; LList next; LList prev; LList rand; LList down; } }
Are there better ways of doing it?
- koustav.adorable in United States| Report Duplicate | Flag | PURGE
Amazon SDE-2 Algorithm - -1of 1 vote
AnswersGiven a fully connected graph with n nodes and corresponding values. One node can interact with other node at a time, to replace/ignore/add its value to other node’s value. Assuming this operation takes 1 unit of time, how much time would it take for all the nodes to have value equal to sum of all the nodes.
- koustav.adorable in United States
Examples : Given a graph with values {1,2,3,4}, find total time it takes, such that all nodes have value as 10.
I am assuming it can be done in O(N).It will take basically two traversals, one for calculating the sum of values of nodes(first traversal), other for replacing the value of the nodes(second traversal).
It will take 2*(no of nodes) time.
Are there any better ways possible ?| Report Duplicate | Flag | PURGE
Amazon SDE-2 Data Structures - -1of 1 vote
AnswersHow can i design a file storage and sharing app like Google drive or Dropbox ?
- koustav.adorable in United States| Report Duplicate | Flag | PURGE
- -1of 1 vote
AnswersThis is the implementation for KMP shift table or the processing of pattern. Is the implementation correct ?Please provide test cases to break the code.
public static String longestPrefSuff(char arr[]) { int k[] = new int[arr.length]; int i = 0, j = 0; for (int m = 0; m < arr.length; m++) { if (m == 0) { j++; k[m] = 0; } else { if (arr[i] == arr[j]) { k[m] = k[m - 1] + 1; i++; j++; } else { i = 0; if (arr[i] == arr[j]) { i++; k[m] = 1; } j++; } } } return Arrays.toString(k);
}
- koustav.adorable in India| Report Duplicate | Flag | PURGE
Software Analyst Algorithm - -1of 1 vote
AnswersTo find the number of groups and output the groups:
- koustav.adorable in United States
Explanation: To find the sum of the elements in the groups and that sum should be divisible by input X and the groups should be limited to range with X numbers.
If X is 3, then the group should have only 2 elements and 3 elements from the array whose sum is divisible by 3.
Input:
Array: 3, 9, 7, 4, 6, 8
X: 3
Output:
3, 9
3, 6
9, 6
3, 9, 6
No of groups: 4| Report Duplicate | Flag | PURGE
Algorithm - 6of 6 votes
AnswersGiven a array int a[]={2,5,1,9,3,7,2,8,9,3} and the no. of swap operations.We are allowed to do swap operations.
swap constraint: exchange only adjacent element.
Find the max number that can be formed using swap operations.
- koustav.adorable in Indiapublic static int[] maximize(int arr[],int swapsAllowed);
| Report Duplicate | Flag | PURGE
Amazon Web Developer Coding
public static void main(String args[]) throws Exception {
printSums(10, 15, 55);
}
public static void printSums(int c1, int c2, int c3) {
BitSet bitSet=new BitSet(1000);
bitSet.set(0);
for (int sum = 1; sum <= 1000; sum++) {
if (bitSet.get(Math.abs(sum - c1)) || bitSet.get(Math.abs(sum - c2)) || bitSet.get(Math.abs(sum - c3))) {
System.out.println(sum);
bitSet.set(sum);
}
}
}
public static void main(String args[]) throws Exception {
}
public TreeNode getCopyNode(TreeNode root, TreeNode copy, TreeNode target) {
TreeNode op = null;
if (getCopyNode(root, copy, target, op))
return op;
return null;
}
public boolean getCopyNode(TreeNode root, TreeNode copy, TreeNode target, TreeNode node) {
if (copy!=null && copy.equals(target)) {
node = copy;
return true;
}
if (root.getChildren() != null) {
int i = 0;
for (TreeNode treeNode : root.getChildren()) {
if (getCopyNode(treeNode, copy.getChildren().get(i++), target, node))
return true;
}
}
return false;
}
static class TreeNode {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((children == null) ? 0 : children.hashCode());
result = prime * result + data;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TreeNode other = (TreeNode) obj;
if (children == null) {
if (other.children != null)
return false;
} else if (!children.equals(other.children))
return false;
if (data != other.data)
return false;
return true;
}
TreeNode(int data) {
this.data = data;
}
@Override
public String toString() {
return this.data + "";
}
int data;
List<TreeNode> children;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
}
}
def countNegInRowColSortedMatrix(matr):
row = 0;
col = len(matr[0]) - 1;
countNeg = 0;
while(row < len(matr) and col >= 0):
while col >= 0 and matr[row][col] >= 0 :
col = col - 1;
countNeg = countNeg + col + 1;
row = row + 1;
return countNeg;
matr = [[-100, -20, -10],
[-5, -7, 0],
[-4, -2, 42]];
print countNegInRowColSortedMatrix(matr);
def countOccurences(x, y):
q = Queue.Queue(x);
q.put(y);
count = 0;
visited = [];
while not q.empty():
item = q.get();
count = count + str(item).count(str(y));
for i in range(0, 10):
temp = str(item) + str(i);
if int(temp) <= x and temp not in visited:
q.put(temp);
visited.append(temp);
for i in range(1, 10):
temp = str(i) + str(item);
if int(temp) <= x and temp not in visited:
q.put(temp);
visited.append(temp);
print count;
countOccurences(40,4);
public static void main(String args[]) {
LList head1 = new LList(19);
head1.next = new LList(450);
head1.next.next = new LList(540);
head1.next.next.next = new LList(640);
LList head2 = new LList(10);
head2.next = new LList(45);
head2.next.next = new LList(504);
LList temp = mergeLists(head1, head2);
while (temp != null) {
System.out.print(temp + " ");
temp = temp.next;
}
}
private static LList mergeLists(LList head1, LList head2) {
if (head1 == null)
return head2;
if (head2 == null)
return head1;
LList temp = null;
if (head1.data < head2.data) {
temp = head1;
temp.next = mergeLists(head1.next, head2);
return temp;
} else {
temp = head2;
temp.next = mergeLists(head1, head2.next);
return temp;
}
}
import Queue
def countOccurences(x, y):
q = Queue.Queue(x);
q.put(y);
count = 0;
while not q.empty():
item = q.get();
count = count + str(item).count('2');
for i in range(0, 10):
temp = str(item) + str(i);
if int(temp) <= x :
q.put(temp);
for i in range(1, 10):
temp = str(i) + str(item);
if int(temp) <= x and i != y:
q.put(temp);
print count;
Can you explain the question giving more examples pls ?
- koustav.adorable June 06, 2017def printInorderFromPreorderStackVersion(nums):
stack = [];
stack.append(nums[0]);
index = 1;
while index < len(nums):
if len(stack) == 0 or stack[len(stack) - 1] > nums[index]:
stack.append(nums[index]);
else:
while len(stack) != 0 and stack[len(stack) - 1] < nums[index]:
print stack.pop();
stack.append(nums[index]);
index = index + 1;
nums = [8, 4, 2, 6, 18, 10, 20]
print(printInorderFromPreorderStackVersion(nums));
def printInorderFromPreorder(nums, start, end):
if start == end:
list = [nums[start]];
return list;
if start > end:
return [];
root = nums[start];
breakPoint = 0;
for i in range(start + 1, end + 1):
if nums[i] > root:
breakPoint = i;
break;
left = printInorderFromPreorder(nums, start + 1, breakPoint - 1);
left.append(root);
right = printInorderFromPreorder(nums, breakPoint, end);
left.append(right);
return left;
nums=[5, 2, 1, 3, 8, 6, 10]
print(printInorderFromPreorder(nums, 0, len(nums)-1));
def printSubsets(items,Sum,tempList,list):
if Sum == 0:
i1= copy.copy(tempList);
list.append(i1);
return;
if 0 == len(items):
return;
index = 0;
items.sort();
i = items[0];
tempList.append(i);
j1= copy.copy(items);
j1.remove(i);
printSubsets(j1,Sum-i,tempList,list);
tempList.remove(i);
printSubsets(j1,Sum,tempList,list);
return list;
tempList=[];
list=[];
print printSubsets([8,2,3,13,11,5],13,tempList,list)
def printPerm(Sum,N,items):
if N == 0 and Sum == 0:
print items
return;
if Sum < 0 or N < 0 :
return;
for i in range(Sum+1):
items.append(i);
printPerm(Sum-i,N-1,items);
items.remove(i);
public static void main(String args[]) {
int[][] arr = {
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 },
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1 }
};
System.out.println(maxOnesInRow(arr));
}
public static int maxOnesInRow(int matr[][]) {
int maxOnes = 1;
int i = matr.length - 1;
int j = 0;
while (i >= 0 && j >= 0) {
while (matr[i][j] == 1)
j--;
while (matr[i][j] == 0)
j++;
maxOnes = Math.max(matr[0].length - j, maxOnes);
i--;
}
return maxOnes;
}
How do you ensure that the same combination i.e (1+5+10 and 10+1+5) is not present in queue ?
- koustav.adorable October 12, 2016How does this work ?
- koustav.adorable October 12, 2016class Main
{
public static void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String arr[])
{
int elem[] =
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
};
int p[] = new int[20];
createPosArr(p);
System.out.println(Arrays.toString(p));
sort(elem, p);
System.out.println(Arrays.toString(elem));
}
public static void createPosArr(int pos[])
{
int i = pos.length - 1;
int j = 0;
while (i - 1 >= 0)
{
pos[i] = j;
pos[i - 1] = (pos.length - 1) - j;
j++;
i = i - 2;
}
}
public static void sort(int a[], int b[])
{
for (int i = 0; i < a.length; i++)
{
while (i != b[i])
{
int c = a[b[i]];
a[b[i]] = a[i];
a[i] = c;
swap(b, i, b[i]);
}
}
}
}
class Practice
{
class NTreeNode
{
int data;
List<NTreeNode> children;
public NTreeNode(int data)
{
this.data = data;
}
public int getData()
{
return data;
}
public void setData(int data)
{
this.data = data;
}
public List<NTreeNode> getChildren()
{
return children;
}
public void setChildren(List<NTreeNode> children)
{
this.children = children;
}
@Override
public String toString()
{
return "" + this.data;
}
}
public static void main(String arr[])
{
// 6 -6 8 4 -12 9 -8 8
Practice prc = new Practice();
NTreeNode node1 = (prc).new NTreeNode(1);
NTreeNode node2 = (prc).new NTreeNode(2);
NTreeNode node3 = (prc).new NTreeNode(3);
NTreeNode node4 = (prc).new NTreeNode(4);
List<NTreeNode> children = Arrays.asList(node2, node3, node4);
node1.setChildren(children);
NTreeNode node5 = (prc).new NTreeNode(5);
NTreeNode node6 = (prc).new NTreeNode(6);
NTreeNode node7 = (prc).new NTreeNode(7);
List<NTreeNode> children1 = Arrays.asList(node5, node6, node7);
node2.setChildren(children1);
NTreeNode node8 = (prc).new NTreeNode(8);
NTreeNode node9 = (prc).new NTreeNode(9);
NTreeNode node10 = (prc).new NTreeNode(10);
List<NTreeNode> children2 = Arrays.asList(node8, node9, node10);
node4.setChildren(children2);
postOrderIterativelyNary(node1);
}
public static void postOrderIterativelyNary(NTreeNode root)
{
Stack<NEntry> stack = new Stack<NEntry>();
Practice prc = new Practice();
stack.push((prc).new NEntry(root, false));
while (!stack.isEmpty())
{
NEntry entry = stack.pop();
NTreeNode node = entry.node;
if (entry.flag == false)
{
if (node.getChildren() == null || node.getChildren().size() == 0)
{
System.out.println(node.data);
} else
{
stack.push((prc).new NEntry(node, true));
List<NTreeNode> children = node.getChildren();
for (int i = children.size() - 1; i >= 0; i--)
{
stack.push((prc).new NEntry(children.get(i), false));
}
}
} else
{
System.out.println(node.data);
}
}
}
class NEntry
{
NEntry(NTreeNode node, boolean flag)
{
this.node = node;
this.flag = flag;
}
NTreeNode node;
boolean flag;
@Override
public String toString()
{
return node.toString();
}
}
}
public static void sort(char a[], int b[])
{
for (int i = 0; i < a.length; i++)
{
while (i != b[i])
{
char c = a[b[i]];
a[b[i]] = a[i];
a[i] = c;
swap(b, i, b[i]);
}
}
}
public static void swap(int arr[], int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String arr[])
{
// 6 -6 8 4 -12 9 -8 8
Practice prc = new Practice();
Node head1 = (prc).new Node(3);
Node head2 = (prc).new Node(4);
Node head3 = (prc).new Node(6);
Node head4 = (prc).new Node(-10);
Node head5 = (prc).new Node(8);
Node head6 = (prc).new Node(9);
Node head7 = (prc).new Node(10);
Node head8 = (prc).new Node(-19);
Node head9 = (prc).new Node(10);
Node head10 = (prc).new Node(-188);
Node head11 = (prc).new Node(20);
Node head12 = (prc).new Node(25);
head1.next = head2;
head2.next = head3;
head3.next = head4;
head4.next = head5;
head5.next = head6;
head6.next = head7;
head7.next = head8;
head8.next = head9;
head9.next = head10;
head10.next = head11;
head11.next = head12;
canceloutStuff(head1);
System.out.print(head1);
}
public static Node canceloutStuff(Node node)
{
List<Node> list = new ArrayList<Node>();
int prevItem = 0;
while (node != null)
{
if (list.size() > 0)
{
int item = node.data;
if ((item * prevItem) < 0)
{
int sum = 0;
int index = -1;
for (int j = list.size() - 1; j >= 0; j--)
{
sum = sum + list.get(j).data;
if ((sum + item) == 0)
{
index = j;
break;
}/*
* else if (sum > item) { break; }
*/
}
if (index != -1)
{
for (int j = index; j < list.size(); j++)
{
list.get(j).data = 0;
}
node.data = 0;
} else
{
prevItem = node.data;
}
list.add(node);
} else
{
list.add(node);
prevItem = node.data;
}
} else
{
list.add(node);
prevItem = node.data;
}
node = node.next;
}
System.out.println(list);
Node prev = null, head = null;
for (Node nod : list)
{
if (nod.data != 0)
{
if (prev != null)
{
prev.next = nod;
}
if (prev == null)
{
head = nod;
}
prev = nod;
}
}
System.out.println(head);
return head;
}
public static void placeZeroAtEnd1(int arr[])
{
int i = 0;
while (true)
{
while (i < arr.length && arr[i] != 0)
{
i++;
}
int j = i + 1;
while (j < arr.length && arr[j] == 0)
{
j++;
}
if (i >= arr.length || j >= arr.length)
{
break;
}
if (j > i)
{
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
i++;
j++;
}
}
System.out.println(Arrays.toString(arr));
}
public static void main(String arr[])
{
System.out.println(count(new String(), 3));
}
public static int count(String str, int count)
{
int sum = 0;
if (count <= 0)
{
System.out.println(str);
return 1;
}
if (str.indexOf('b') == -1)
{
sum = sum + count(str + "a", count - 1);
sum = sum + count(str + "b", count - 1);
if (str.length() > 0 && str.charAt(str.length() - 1) != 'c')
{
sum = sum + count(str + "c", count - 1);
} else if (str.length() == 0)
{
sum = sum + count(str + "c", count - 1);
}
} else
{
sum = sum + count(str + "a", count - 1);
if (str.length() > 0 && str.charAt(str.length() - 1) != 'c')
{
sum = sum + count(str + "c", count - 1);
} else if (str.length() == 0)
{
sum = sum + count(str + "c", count - 1);
}
}
return sum;
}
int sum1=0,sum2=0;
Set<Integer> set=new HashSet<Integer>();
int p=arr.length;
boolean result=false;
for(int i=0;i<=p/2;i++)
{
sum1=sum1+arr[i];
}
for(int i=(p/2)+1;i<arr.length;i++)
{
sum2=sum2+arr[i];
set.add(arr[i]);
}
result=set.contains(sum2-sum1);
if(!result)
{
int sum1=0,sum2=0;
set.clear();
for(int i=(p-1);i>(p/2);i--)
{
sum1=sum1+arr[i];
}
for(int i=(p/2);i>=0;i--)
{
sum2=sum2+arr[i];
set.add(arr[i]);
}
return set.contains(sum2-sum1);
}
else
{
return true;
}
Merge sort is the solution. Since this is a linked list(not a array), we don't need O(N) space. We can simply change the pointers while merging them.
- koustav.adorable December 26, 2015public static boolean placeOnes(int arr[], int start, int n)
{
if (n == 0)
return true;
if (start >= arr.length)
return false;
for (int index = start; index < arr.length; index++)
{
if (arr[index] == 0 &&
((index == 0 && arr[index + 1] == 0) || (index == arr.length - 1 && arr[index - 1] == 0) ||
(index >= 1 && index <= arr.length - 2 && arr[index - 1] == 0 && arr[index + 1] == 0)))
{
arr[index] = 1;
if (placeOnes(arr, index + 1, n - 1))
return true;
else
{
arr[index] = 0;
}
}
}
return false;
}
public static void main(String[] args)
{
System.out.println(maxLen(Integer.MAX_VALUE, Integer.MIN_VALUE, 0, 0));
}
public static int maxLen(int min, int max, int len, int index)
{
if (index >= arr.length && Math.abs(max - min) != 1)
{
return Integer.MIN_VALUE;
}
if (index >= arr.length && Math.abs(max - min) == 1)
{
return len;
}
int orgMax = max;
int orgMin = min;
if (arr[index] <= min)
min = arr[index];
if (arr[index] >= max)
max = arr[index];
int p = maxLen(min, max, len + 1, index + 1);
int q = maxLen(orgMin, orgMax, len, index + 1);
return Math.max(p, q);
}
What does the max and map.end() function do ?
I am a java person.I am not used to c++ map.
Execellent
- koustav.adorable May 12, 2014
RepHi, I’m Jamie from the Portsmouth USA and I am working as an account manager. I work for a ...
RepDonnaWHale, Data Engineer at ADP
Hi, I am passionate writer with a BA in English from the Ohio State University.5+ years of experience writing ...
- koustav.adorable July 23, 2017