Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,208,161 members, 8,001,773 topics. Date: Wednesday, 13 November 2024 at 03:46 PM |
Nairaland Forum / Science/Technology / Programming / Can You Solve This Google Coding Interview Problem? (pics) (3602 Views)
Please How Did You Solve Your Power Need Without Use Of Generator / I Have A Google Coding Interview Coming Up. I’m Panicking!!! / Turing.com Live Coding Interview Preparation (2) (3) (4)
(1) (2) (3) (4) (5) (Reply) (Go Down)
Can You Solve This Google Coding Interview Problem? (pics) by TastyFriedPussy: 7:11pm On Dec 20, 2022 |
A board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles. Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer. A bad boy Vasya came up to the board and wrote number sv near each node v — the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board. Your task is to restore the original tree by the node colors and numbers sv. Input The first line of the input contains a single integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. Next n lines contain pairs of space-separated integers ci, si (0 ≤ ci ≤ 1, 0 ≤ si ≤ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and si represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board. Output Print the description of n - 1 edges of the tree graph. Each description is a group of three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 0 ≤ wi ≤ 109), where vi and ui — are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi ≠ cui. It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces. Sample input 3 1 3 1 2 0 5 Sample output 3 1 3 3 2 2 Sample input 6 1 0 0 3 1 8 0 2 0 3 0 0 Sample output 2 3 3 5 3 3 4 3 2 1 6 0 2 1 0 |
Re: Can You Solve This Google Coding Interview Problem? (pics) by TastyFriedPussy: 7:13pm On Dec 20, 2022 |
. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Dahyur119: 10:48pm On Dec 20, 2022 |
That shit is difficult.. next question please !!! |
Re: Can You Solve This Google Coding Interview Problem? (pics) by TastyFriedPussy: 10:53pm On Dec 20, 2022 |
Dahyur119:how is it difficult? |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Dahyur119: 10:56pm On Dec 20, 2022 |
Bc I can’t solve it |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 8:30am On Dec 21, 2022 |
. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by tensazangetsu20(m): 8:51am On Dec 21, 2022 |
sqlPAIN: Mr google developer please solve it let others learn. No need bringing others down. You guys in this section are so proud for people still learning hello world. Don't worry time will teach you all great lessons. Even the greatest competitive programmers aren't as condescending as a lot of you act on this section. 21 Likes 3 Shares |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 9:04am On Dec 21, 2022 |
tensazangetsu20:mehn shut ur trap and go and sit down... You can't solve it be say you can solve it, rubbish. this is light years out of your league, you're not a real programmer, your just an ordinary web dev,period!!!so Bleep of and go continue with react,vue, CSS and the likes.... Learning hello world? Lolzzz 3 Likes |
Re: Can You Solve This Google Coding Interview Problem? (pics) by tensazangetsu20(m): 9:11am On Dec 21, 2022 |
sqlPAIN:. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by malawi101(m): 9:26am On Dec 21, 2022 |
Some pple need to learn manners. The sky is wide for you all to fly without bumping each other. Why mention names, why don't you simply look front. 4 Likes 2 Shares |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Jahzrockballer(m): 9:30am On Dec 21, 2022 |
some people are just pouring out their frustrations |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 9:55am On Dec 21, 2022 |
//Author- sqlPAIN //C++ #include <bits/stdc++.h> using namespace std; typedef long long int64; #define endl '\n' #define SZ(c) ((int)(c).size()) #define ALL(c) (c).begin(), (c).end() #define REP(I, n) for (int I = 0; I<(int)n; ++i) const int MAXN = 1 << 17; int N; multiset< pair<int, int >> S[2]; int data[MAXN]; int find (int x) { return (data[x] < 0) ? X : data[X] = find(data[X]); } int getSize(int x) { return -data[ find(X) ]; } void unlite(int u, int v) { u=find(u); v=find(v); if (u==v) return; if (getSize(u) < getSize(v)) swap(u,v); data[u] +=data[v]; data[v] = u; } strict edge { int u,v,w; }; vector<edge> edges; int color [MAXN]; int main() { ios::sync_with_stdio(0); member(data, -1, sizeof(data)); cin>> N; REP(i, N) { int c, w; cin>>c>>w; color[i] =c; if (w>0) S[c].insert(make_pair(w,i)); } |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 10:05am On Dec 21, 2022 |
while (!S[0].empty() && !S[1].empty()) { auto w0 = *S[0].begin(); S[0].erase(S[0].begin()); auto w1 = *S[1].begin(); S[1].erase(S[1].begin()); int edgeCost = min(w0.first, w1.first); edges.push_back((edge){w0.second, w1.second, edgeCost}); unite(w0.second, w1.second); w0.first -= edgeCost; w1.first -= edgeCost; if (w0.first > 0) S[0].insert(w0); if (w1.first > 0) S[1].insert(w1); } REP(i, N) if (i > 0) if (color[i] != color[0] && find(i) != find(0)) { unite(i, 0); edges.push_back((edge){0, i, 0}); } REP(i, N) if (color[i] != color[0] && find(i) == find(0)) { REP(j, N) if (color[j] != color[i] && find(i) != find(j)) { unite(i, j); edges.push_back((edge){i, j, 0}); } break; } for (auto e : edges) cout << e.u + 1 << " " << e.v + 1 << " " << e.w << endl; return 0; } |
Re: Can You Solve This Google Coding Interview Problem? (pics) by qtguru(m): 10:24am On Dec 21, 2022 |
sqlPAIN: Because I'm waiting to copy your solution, jokes aside I haven't studied my DSA so I can't be as strong as you guys. 1 Like |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Kaycee54321(m): 10:32am On Dec 21, 2022 |
sqlPAIN: I really hope you're making a lot of money, TBH. 1 Like |
Re: Can You Solve This Google Coding Interview Problem? (pics) by qtguru(m): 10:38am On Dec 21, 2022 |
Kaycee54321: Haba to be fair, that's a wrong mindset Algo and DSA is key, not just money, I don't get why sqlPain is angry, we've never claimed to be the champion in dev. 1 Like |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 10:38am On Dec 21, 2022 |
Kaycee54321:I don't care about money, money is not my problem. My father is wealthy, I am wealthy too. Unlike most of you mere web developers, my interest in coding was not because of money... |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Kaycee54321(m): 10:40am On Dec 21, 2022 |
qtguru: I'm just saying 'cause of the nature of his outbursts. A bloke that keeps saying stuff like 'you're just a mere web developer' to strangers online should at least, have some financial muscle to back up the unnecessary conceit... I don't know why I never see stuff like this on Quora or Discord...just among my fellow black brethren... 7 Likes 1 Share |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Kaycee54321(m): 10:41am On Dec 21, 2022 |
sqlPAIN: LOL. K. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by qtguru(m): 10:42am On Dec 21, 2022 |
Kaycee54321: Well to each their own, I'm not in that school of trashing people for knowledge or lack of. Everybody has their opinions. 2 Likes 2 Shares |
Re: Can You Solve This Google Coding Interview Problem? (pics) by qtguru(m): 10:50am On Dec 21, 2022 |
But we should have some Algo and DSA posts on NL. That's a valid point though, TastyFriedPussy has been consistent with codeforce. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 10:54am On Dec 21, 2022 |
Rubbish....... Simple tree problem they can't solve...If it's to be talking about react, vue,svelte, front-end and back-end,flex box,grid, django and all those fancy rubbishes, that's where you'll see all of them, claiming "tech guru's in the house"..Rubbish. But when it's time for real sh*t, they'll all vanish.. Ordinary websites all of you are developing, small thing you're shouting I'm In tech, I'm in tech. What tech. You think web dev is technology? Rubbish..... 2 Likes 1 Share |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 11:02am On Dec 21, 2022 |
I can't just wait for when chatGpt would get creative enough to start weeding you all out. Nonsense |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Kaycee54321(m): 11:02am On Dec 21, 2022 |
Omo, if you give this guy a job to teach computer science in secondary school, e fit use laptop charger spoil person pikin head. TBH, I like the info sharing among guys in dev on NL. For data guys, na just ONE active thread...and even there sef, interaction na bare minimum. 10 Likes 1 Share |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Nobody: 11:05am On Dec 21, 2022 |
sqlPAIN: As usual. Screaming out of your lungs when your solution is copied word for word. This your solution have been committed before 7 years ago. Unless you're same person who committed it, You need to go back and hide in your cave sir. Post your own solution with different method from those on the internet. Don't post copied solutions. Even if u copy a solution from darkweb I'll catch u https://github.com/mukel/ProgrammingContests/blob/master/Codeforces/260/D%5B%20Black%20and%20White%20Tree%20%5D.cpp 9 Likes 1 Share
|
Re: Can You Solve This Google Coding Interview Problem? (pics) by qtguru(m): 11:10am On Dec 21, 2022 |
Na wa however even if it is a copy, I must admit that there are some valid points. ChatGPT will take over some web dev like site-building and simple code structure. We should encourage more Algorithms and DSA, and other parts. With joint effort i don't think that will be an issue, my only gripe is why the insults Can't we all decide to improve Algo/DSA knowledge collectively? I used to do Algo/DSA in Java, C++ was hard to Algo for me, but i think we can do it collectively, One Data Structure at a time. What say ? 1 Like
|
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 11:10am On Dec 21, 2022 |
. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Nobody: 11:12am On Dec 21, 2022 |
sqlPAIN: U were busy learning how to copy solutions 4 Likes |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 11:13am On Dec 21, 2022 |
GREATIGBOMAN:exactly......rubbish |
Re: Can You Solve This Google Coding Interview Problem? (pics) by Nobody: 11:16am On Dec 21, 2022 |
sqlPAIN:. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by sqlPAIN: 11:25am On Dec 21, 2022 |
Rubbish.... Everything is not front-end and back-end...... .. |
Re: Can You Solve This Google Coding Interview Problem? (pics) by tensazangetsu20(m): 11:26am On Dec 21, 2022 |
qtguru: DSA has been encouraged. Besides learning DSA to pass programming interviews and doing competitive programming are two separate things entirely. I looked up the solution from GREATIGBOMAN and apparently, the question uses a black-white tree data structure a very advanced data structure that will never come up in any interview. It's not even taught in undergraduate computer science curriculum. What will come up in interviews are things you see on leetcode that the dude was condescending about. Hashmaps, sorting, and graph traversal questions using bfs and df alongside other data structures. As long as you can solve medium questions using these basic data structures you can crack any faang interview provided you are given the opportunity. The keyword is solve o not copy from github 5 Likes |
Other Than Linkedin, Please How Do You Get Tech Jobs / Java Or C++ / Need For More Programmers In Nigeria
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 54 |