Sap Labs Interview Questions
- 0of 0 votes
AnswerSuppose we have some input data describing a graph of relationships between parents and children over multiple generations. The data is formatted as a list of (parent, child) pairs, where each individual is assigned a unique positive integer identifier.
<p>
For example, in this diagram, 3 is a child of 1 and 2, and 5 is a child of 4:
<p>
1 2 4 15
\ / / | \ /
3 5 8 9
\ / \ \
6 7 11
<p>
<p>
Sample input/output (pseudodata):
<p>
parentChildPairs = [
(1, 3), (2, 3), (3, 6), (5, 6), (15, 9),
(5, 7), (4, 5), (4, 8), (4, 9), (9, 11)
]
<p>
<p>
Write a function that takes this data as input and returns two collections: one containing all individuals with zero known parents, and one containing all individuals with exactly one known parent.
<p>
<p>
Output may be in any order:
<p>
findNodesWithZeroAndOneParents(parentChildPairs) => [
[1, 2, 4, 15], // Individuals with zero parents
[5, 7, 8, 11] // Individuals with exactly one parent
]
<p>
n: number of pairs in the input
- Manoj May 04, 2021 in India for Senior Software Engineerpublic static void main(String[] argv) { int[][] parentChildPairs = new int[][]{ {1, 3}, {2, 3}, {3, 6}, {5, 6}, {15, 9}, {5, 7}, {4, 5}, {4, 8}, {4, 9}, {9, 11}}; findNodesWithZeroAndOneParents(parentChildPairs); } public static void findNodesWithZeroAndOneParents(int[][] parentChildPairs) { Map<Integer, List<Integer>> childAncestor = new HashMap<>(); for (int[] parentChildPair : parentChildPairs) { int parent = parentChildPair[0]; int child = parentChildPair[1]; if (!childAncestor.containsKey(child)) { childAncestor.put(child, new ArrayList<>()); } List<Integer> parentValue = childAncestor.get(child); parentValue.add(parent); childAncestor.put(child, parentValue); } System.out.println(childAncestor.toString()); dfs(childAncestor); } private static void dfs(Map<Integer, List<Integer>> childAncestor) { Set<Integer> childerOneParent = new HashSet<>(); Set<Integer> parentWithOutParent = new HashSet<>(); for (Map.Entry<Integer, List<Integer>> parent : childAncestor.entrySet()) { if (parent.getValue().size() == 1) { childerOneParent.add(parent.getKey()); } List<Integer> part = parent.getValue(); for (Integer p : part) { if (!childAncestor.containsKey(p)) { parentWithOutParent.add(p); } } } System.out.println(childerOneParent.toString() + " / " + parentWithOutParent.toString()); } }
| Report Duplicate | Flag | PURGE
Sap Labs Senior Software Development Engineer Graphics - 0of 0 votes
AnswersA Fibonacci sequence is defined recursively by:
- Manoj September 17, 2020 in India for Senior Software Engineer
F0 = 0
F1 = 1
Fn = Fn − 1 + Fn − 2, for integer n > 1.
One way of generalizing the Fibonacci sequence is by starting with any pair of numbers and extending to negative values of n.
Given two terms of a generalized Fibonacci sequence Fp and Fq, their positions p and q respectively and a position r, find Fr.
Input Format
The first line of the input contains an integer t denoting the number of test cases.
Each test case contains three lines.
First line of each test case contains two space separated integers p and Fp
Second line contains two space separated integers q and Fq
Third line contains an integer r
Output Format
For each test case, print Fr which is the term of the sequence at position r.
If Fr is not an integer, represent it as an irreducible fraction in the form a/b where b > 0.
Sample Input
0 1
6 13
10
3 65
6 315
-10
0 11
1 -6
2
9 36
15 646
-5
11 72
20 5473
6
Sample Output
89
4620
5
-1/4
13/2| Report Duplicate | Flag | PURGE
Sap Labs Senior Software Development Engineer Java - 0of 0 votes
AnswersMickey got an assignment from his school that he has to collect money from each house for next day's event. The city is built just like a 2-D array and each house owner kept certain amount for Mickey (for ex: The house owner at (i,j) coordinate kept A[i,j] for Mickey). Mickey is in a hurry and he wants to reach home as soon as possible otherwise he will miss the match between India and Bangladesh. Also he can only move to any adjacent house from his location, and he cannot move diagonally. So please find a way so that he will collect maximum amount from everyone if he will reach home earliest.
*Input Specification*
input1 = first input will tell you size of the array
input2 = String as shown in the example and you have to parse it to build your 2-D array
*Output Specification*
output1 = You need to set maximum amount collected to this global variable (type int)
*Example*
Consider input1 = 4 and it means school is at (0,0) and his house is at (4,4). In between the houses are like input2 = '(1,7,5,2),(5,12,3,6),(100,9,23,16),(16,4,5,9)'.
To collect maximum amount, Mickey should take his path {1,5,100,9,23,16,9} so that the total amount collected,output1 = 1 + 5 + 100 + 9 + 23 + 16 + 9 = 163
**CLARIFICATION **
The programming test was like this, you store the input into the variablesinput1
and
input2
and store the output to the variable
- invincible1000 August 22, 2016 in United Statesoutput1
| Report Duplicate | Flag | PURGE
Sap Labs Algorithm - 0of 0 votes
AnswersIn Mathematics each number has one special number, which it supports, chosen as follows. It counts the number of ones in its own binary representation, and adds this count to itself to obtain the value of the number it supports. That is, if j(m) means the number of ones in the binary representation of m, then m supports m+j(m). For example, the number eight (1000 in binary) supports nine, whereas nine supports eleven.
- anonymous June 16, 2014 in India
However, in this way not all the numbers get supported; some are left without support, and we call these numbers bleak. For example since one supports two, two supports three and three supports five, there is no number less than four, which would support four, so four is bleak.
Your task is for a given number recognize if it is bleak or supported by some number.| Report Duplicate | Flag | PURGE
Sap Labs Software Engineer / Developer Coding - 0of 0 votes
AnswersWhat is wrong with the below code?
- Aashish June 29, 2012 in India#include <iostream> #include <string.h> using namespace std; class A { char *p; public: A(const char* str) { p=new char[strlen(str)+1]; strcpy(p,str); } ~A() { delete p; } }; int main() { A s("Object s"); A t=s; s.~A(); A u("Object u"); u=s; return 0; }
| Report Duplicate | Flag | PURGE
Sap Labs Software Engineer / Developer Algorithm - 0of 0 votes
AnswersThe second round interview: Implement a binary tree with the given interface, then discuss the implementation via remote desktop and phone.
- groupme May 29, 2012 in United States for Potsdam/* * An interface for an sorted binary tree. * * The interface provides methods for inserting values, checking if certain values are contained and iterating over the elements. * Note: Implementing classes should provide an iterator that traverse the inserted object in the sorted order. */ public interface IBinTree<V extends Comparable<V>> extends Iterable<V> { /* * Insert an object into the binary tree. Note: The tree should be sorted, inserting the same object twice is allowed but the insert is expected to be stable. */ void insert(V obj); /* * Batch-insert multiple elements. */ void insert(Vector<V> vec); /* * Check if the object is already in the tree. Return true if it is, false otherwise. */ boolean contains(V obj); }
| Report Duplicate | Flag | PURGE
Sap Labs Developer Program Engineer Trees and Graphs - 0of 0 votes
AnswersThe first round: Phone interview + online coding:
- groupme May 29, 2012 in Germany for Potsdam
You are given an array of n integers, each of which may be positive, negative or zero. Give an algorithm to identify the start and end index, i and j, of the interval whose elements form the maximal sum of all possible intervals. Assume j >=i
e.g. {1 3 -8 2 -1 10 -2 1} -> i=3 , j=5 – sum = 11
Example non-maximal sum intervals:
i=0, j=5 – sum = 7
i=2, j=4 – sum = -7| Report Duplicate | Flag | PURGE
Sap Labs Developer Program Engineer Algorithm - 0of 0 votes
Answershow exactly virtual table and virtual pointer functions.
- kiit.manu October 15, 2011 in India| Report Duplicate | Flag | PURGE
Sap Labs Software Engineer / Developer - 0of 0 votes
Answerswrite a program to reverse a linked list.
- kiit.manu October 15, 2011 in India| Report Duplicate | Flag | PURGE
Sap Labs Software Engineer / Developer - 0of 0 votes
Answerscreate 4 equilateral triangle using 6 match sticks.
- kiit.manu October 15, 2011 in India| Report Duplicate | Flag | PURGE
Sap Labs Software Engineer / Developer