Skip to main content
26 votes
Accepted

Google Foobar Challenge: Lucky Triples

1. Testing Let's take a look at your second problem: I've tested it on rather trivial cases, but have no way of knowing [...] if it returns the correct answer in more intricate examples A good ...
Gareth Rees's user avatar
  • 49.6k
21 votes
Accepted

Extraction of value from a dict-tree, given a path of arbitrary length

Recursive Approach Review In this method you are performing too many look-ups. First, you are looking up the 0th value of p two or three times. List indexing ...
AJNeufeld's user avatar
  • 33.8k
17 votes
Accepted

Printing binary trees

First of all, in my opinion, both the algorithm is nice and interesting, and the code is really well-written, broken down into easily understandable functions! Well done! The only addition that I can ...
Attilio's user avatar
  • 1,645
16 votes
Accepted

Binary Tree Implementation in Rust

Examples have first-class support in Cargo. Place them in a directory examples next to src. They can then be run with ...
Shepmaster's user avatar
  • 8,658
15 votes

Building a Tree from a flat List<Nodes>

Well your provided example data can't be real, because I never ever saw an int? which could take 1.1 as value. I will apply ...
Heslacher's user avatar
  • 50.4k
12 votes

Extraction of value from a dict-tree, given a path of arbitrary length

Use a library. In particular, glom does an admirable job of this, and is actively developed. ...
Cireo's user avatar
  • 239
11 votes
Accepted

Using smart pointers to construct Binary Search Tree

Answers to your questions Am I using std::unique_ptr correctly here? Using std::move and get? Yes, you are using those correctly. Is there any way for me to make ...
G. Sliepen's user avatar
  • 58.3k
10 votes
Accepted

Trie Tree match string containing ? and *

1. Review The implementation with defaultdict is very elegant. I like it. The code is not portable to Python 3 because of the use of the ...
Gareth Rees's user avatar
  • 49.6k
10 votes
Accepted

BTreeMap as a multimap in Rust

Idiomatic Rust style is to place braces on same line. When the function definition gets longer than one line then the brace lives on the next line all on its own. Learn and love the ...
Shepmaster's user avatar
  • 8,658
10 votes
Accepted

Use generator to do inorder traversal

Your structure seems adequate, but there are a couple of mistakes, and some new language syntax in 3.3 that will make things even clearer: That name! ...
aghast's user avatar
  • 12.4k
10 votes
Accepted

Binary Search Tree Data Structure Implementation in C++11 using Smart Pointers

I would put the node inside the tree as a private member. ...
Loki Astari's user avatar
  • 95.3k
10 votes
Accepted

Generic Binary Tree

Use std::unique_ptr! I see two potential memory leaks in the code, and both of them would have been prevented by using ...
hoffmale's user avatar
  • 6,498
10 votes
Accepted

Recursive search on Node Tree with Linq and Queue

Reading Material .. any reading material about searching algorithms on trees These are the most common tree walkers: Breadth-First Search Depth-First Search Review There is a bug with IsRoot. ...
dfhwze's user avatar
  • 13.9k
10 votes
Accepted

Iterator for traversing a tree [v2]

My problem with this iterator is that it is very expensive to copy; as you have to copy a stack when you copy the iterator. The main point of iterator is that it should be "cheap" to make copies. All ...
Loki Astari's user avatar
  • 95.3k
10 votes
Accepted

Simple tree dictionary structure in C

Hide the implementation: Node should be an opaque data type defined in the source file. The header file would then only contain the forward declaration, and the ...
Harith's user avatar
  • 9,040
9 votes

Building a Tree from a flat List<Nodes>

(Bug) Edit: It seems that your incoming data are sorted. If so, this 'bug' is not valid of course Your traversing algorithm works fine as long as the TreeNodes ...
JanDotNet's user avatar
  • 8,538
9 votes

Binary Search Tree BFS iteratively

Much of the code looks good. Some comments could be clearer, and there are some correctness issues. In is_leaf(), please strike this redundant remark: ...
J_H's user avatar
  • 29.3k
9 votes
Accepted

Implementing a binary tree in C++ using "std::unique_ptr"

On raw and smart pointers First, I'll address your specific question, about using a raw pointer to refer to nodes in the tree. I believe you're doing exactly the right thing here, because smart ...
Toby Speight's user avatar
  • 77.1k
9 votes

Iterator for traversing a tree [v2]

Martin York already has a very good point about the iterator, so I'll not comment on that. Avoid writing types twice In main.cpp you are writing statements like: ...
G. Sliepen's user avatar
  • 58.3k
8 votes
Accepted

Inorder, Preorder, and Postorder traversal of a binary tree in C++

I can see a few changes that at least might be worth considering. Minimize Requirements All else being equal, you typically want to minimize the requirements you place on the type contained in a ...
Jerry Coffin's user avatar
  • 33.6k
8 votes
Accepted

Code smell when recursively returning a List

I wouldn't say there is a code smell with what you have now. However, it could be made more OOP-friendly. An actual problem is that you're using raw types for Node,...
Tunaki's user avatar
  • 9,233
8 votes

C++ Tree Class implementation

Some quick comments: Const correctness You really should make your code const correct. For example at least these functions could be const declared: ...
Emily L.'s user avatar
  • 16.6k
8 votes
Accepted

Iterator for BFS binary tree traversal

I'll ignore the 'PluralSight' code. Note that you have implemented a breadth-first traversal. As far as I'm concerned, a spec which doesn't specify how the tree is traversed is insufficient, but you ...
VisualMelon's user avatar
  • 7,431
8 votes

An array with \$O(\log\ n)\$ time complexity for all operations

Import import java.util.*; The class does not need to import the whole java.util package. Instead it only needs: ...
Roman's user avatar
  • 2,843
7 votes

Google Foobar Challenge: Lucky Triples

Initialising an attribute empty and then mutating it in a function is hard to understand. Instead make the functions create the object, and then assign it to the attribute. Your comments ...
Peilonrayz's user avatar
  • 43.4k
7 votes

Google Foobar Challenge: Lucky Triples

Peilonrayz already mentioned this, but I feel this needs more emphasis: I'd argue that the problem is not well suited for implementation as a class. First hint: The challenge says "write a ...
pixelbrei's user avatar
  • 264
7 votes
Accepted

Google Foobar Challenge: Ion Flux Relabeling

In your own words: "I totally understand that my method is not the most efficient solution." In a sense, that's the summary of a code review for this problem. The basic issue in your code is that you ...
rolfl's user avatar
  • 97.4k
7 votes
Accepted

Using range minimal query for lowest common ancestor (LCA)

This review describes my thoughts as a fellow competitive programmer. About your questions In-order traversal is a must here. Otherwise the RMQ-based algorithm won't work - it requires that for each ...
yeputons's user avatar
  • 291
7 votes

Check if two binary trees are equal

Exit the check early Basically, you have a recursive traversal operation that traverses the two input trees sort of in parallel. The advantage of that approach is that you may exit algorithm as soon ...
coderodde's user avatar
  • 28.7k

Only top scored, non community-wiki answers of a minimum length are eligible