Manoj
BAN USER
- 0of 0 votes
AnswersGiven the changes to SAP stock price over a period of time as an array of distinct integers, count the number of spikes in the stock price which are counted as k-Spikes.
- Manoj in India
A k-Spike is an element that satisfies both the following conditions:
There are at least k elements from indices (0, -1) that are less than prices[i].
• There are at least k elements from indices (i+1, n-1) that are less than prices[i].
Count the number of k-Spikes in the given array.
Example
prices = [1, 2, 8, 5, 3, 4] k = 2
There are 2 k-Spikes:
• 8 at index 2 has (1, 2) to the left and (5, 3, 4) to the right that are less than 8.
• 5 at index 3 has (1, 2) to the left and (3, 4) to the right that are less than 5.
Output is : 2| Report Duplicate | Flag | PURGE
Sap Labs Senior Software Development Engineer Data Structures - 0of 0 votes
AnswersPrint the most near missing integer in the unsorted value.
- Manoj in Indiapublic static void main(String args[]) { System.out.println(solution(new int[]{-4,-2})); } private static int solution(int[] ints) { //1,2,3,4 //2,3,4 = 1 //-2,-1,2,3,4 = 0,0,2,3,4 = 1 //-4,-3,-2 = 1 //-4,-2 = 1 //-3,-2,-1 , 1 = 2 Arrays.sort(ints); //O(log*n) for (int i = 0; i < ints.length; i++) { if (ints[i] > 0) { if (ints[i + 1] - ints[i] > 1) { return 1; } break; } ints[i] = 0; } //O(n-m) n- lenght array , m - negative interger //1,2,3,5,7,8,9,10 for (int i = 0; i < ints.length - 1; i++) { if (ints[i + 1] - ints[i] > 1) { return ints[i] + 1; } } //O(n-k) return ints[ints.length - 1] + 1; //O(log n * n2) }
| Report Duplicate | Flag | PURGE
Walmart Labs Staff Engineer Data Structures - 0of 0 votes
AnswersSay that you have p1(Child) - > a & b and p2 (Child) -> c & d. Which return list p1 and p2 child and see p1 and p2 are related. We need to check if they going over the tree.
- Manoj in India
Inupt assumption is : p1 -> a & b
p2 -> c & d
a - it own parent & b - it own parent
c - it own parent & d - it own parent and so on.| Report Duplicate | Flag | PURGE
Salesforce Software Engineer / Developer - 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 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 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 - -1of 1 vote
AnswersYou are given a series of arithmetic equations as a string, such as:
- Manoj in India
y = x + 1
5 = x + 3
10 = z + y + 2
The equations use addition only and are separated by newlines. Return a mapping of all variables to their values. If it's not possible, then return null. In this example, you should return:
{
x: 2,
y: 3,
z: 5
}| Report Duplicate | Flag | PURGE
Google Software Engineer Problem Solving
public class PersonRelation {
public boolean getParentLst(Person child1, Person child2) {
if ((child1 == null && child2 != null) || (child1 != null && child2 == null)) {
return false;
}
List<Person> parent1 = child1.getParent();
List<Person> parent2 = child2.getParent();
Set<Person> child1Parent = bfsGraph(parent1);
Set<Person> child2Parent = bfsGraph(parent2);
return child1Parent.contains(child2Parent);
}
private Set<Person> bfsGraph(List<Person> personList) {
Set<Person> child1Parent = new HashSet<>();
if (!personList.isEmpty()) {
Queue<Person> bfsQueue1 = new LinkedList<>();
bfsQueue1.add(personList.get(0));
while (bfsQueue1.isEmpty()) {
Person p = bfsQueue1.poll();
if (p != null) {
child1Parent.add(p);
List<Person> grandParent = p.getParent();
for (Person gp : grandParent) {
bfsQueue1.add(gp);
}
}
}
}
return child1Parent;
}
}
class Person {
int id;
int parentID;
int relation;
public Person(int id, int parentID, int relation) {
this.id = id;
this.parentID = parentID;
this.relation = relation;
}
static List<Person> parent = new ArrayList<>();
static List<Person> getParent() {
return parent;
}
}
{{public boolean getParentLst(Person child1, Person child2) {
if ((child1 == null && child2 != null) || (child1 != null && child2 == null)) {
return false;
}
List<Person> parent1 = child1.getParent();
List<Person> parent2 = child2.getParent();
Set<Person> child1Parent = bfsGraph(parent1);
Set<Person> child2Parent = bfsGraph(parent2);
return child1Parent.contains(child2Parent);
}
private Set<Person> bfsGraph(List<Person> personList) {
Set<Person> child1Parent = new HashSet<>();
if (!personList.isEmpty()) {
Queue<Person> bfsQueue1 = new LinkedList<>();
bfsQueue1.add(personList.get(0));
while (bfsQueue1.isEmpty()) {
Person p = bfsQueue1.poll();
if (p != null) {
child1Parent.add(p);
List<Person> grandParent = p.getParent();
for (Person gp : grandParent) {
bfsQueue1.add(gp);
}
}
}
}
return child1Parent;
}
}
class Person {
int id;
int parentID;
int relation;
public Person(int id, int parentID, int relation) {
this.id = id;
this.parentID = parentID;
this.relation = relation;
}
static List<Person> parent = new ArrayList<>();
static List<Person> getParent() {
return parent;
}
}}}
Better written code :
- Manoj May 08, 2021