34,111 questions
-2
votes
0
answers
116
views
Is it faster to calculate integer exponentiation with binary or ternary chunks? [duplicate]
Code 1
In code one we are using the binary exponentiation.
public class Solution {
public int pow(int A, int B, int C) {
if (C == 1) return 0;
long base = (A % C + C) % C;
...
3
votes
1
answer
137
views
Getting the same result -> hot potato game in C using Deque
I’m currently learning about data structures and algorithms (DSA) by reading the book “Learning JavaScript Data Structures and Algorithms – 2nd Edition” by Loiane Groner. I am implementing the DSAs in ...
2
votes
1
answer
103
views
Confused about implementing degree() and incident_edges() for topological sort in Python
I am a student studying graph algorithms and learning topological sorting of a Directed Acyclic Graph (DAG).
The topological_sort function below is provided in our textbook, and I am not allowed to ...
Advice
1
vote
4
replies
156
views
Best resources to learn Data Structures, Algorithms, and Big-O from scratch (for Python)
I want to learn Data Structures, Algorithms, and Big-O notation from scratch, but there are too many resources online and it’s difficult to determine which ones are trustworthy or widely used by ...
-2
votes
1
answer
78
views
"TypeError: <class> is not reversible" -- what does it mean?
Something called reversed() on an instance of a class of mine, which resulted in the error
TypeError: 'HBox' object is not reversible
What does it mean for an object to be reversible?
-4
votes
0
answers
45
views
Reverse an Array by recursion using swap in python [duplicate]
so I was trying to reverse an array by recursion so the code logic is we are swaping right most end value to the left most end. I have implemented logic but the thing is My recursion function retuns ...
1
vote
1
answer
76
views
During red–black tree insertion fix-up, when (if ever) does the black-height of nodes change?
I’m studying red–black trees (CLRS style) and I’m confused about how black-height behaves during RB-INSERT-FIXUP.
I quote here that procedure from CLRS:
RB-INSERT-FIXUP(T, z)
while z.p.color == ...
0
votes
1
answer
102
views
How can I efficiently map and update frequencies of nested items using dictionaries in Python?
I'm working on a small DSA practice problem involving frequency counting, but with a twist.
I have a list of tuples where each tuple represents a (category, item). Example:
data = [
("fruit&...
Advice
0
votes
1
replies
43
views
Name for this data structure
I'm trying to model a generic "capability" or "permission" structure. The part I'm having trouble with is finding the name of the specific data structure. It's not a formal tree, ...
1
vote
1
answer
220
views
How to implement a list with an efficient "index of" operation?
I'm interesting in possible implementation approaches for a quite special variant of a list, with the following requirements:
Efficient inverse lookup ("index of"): "give me an index ...
0
votes
2
answers
166
views
MRU structure with fixed number of elements, automatically adapting to LINQ queries
I have a folder containing huge numbers of files in many subfolders, and I want to select the files with a name that matches any of a specified list of patterns (regular expressions). My regular ...
-3
votes
1
answer
193
views
Monotonic Stack Algorithm: Is the Average Time Complexity θ(n) or O(n)? [closed]
The algorithm I've written for an assignment is closely related to this monotonic stack approach
https://www.geeksforgeeks.org/dsa/next-greater-element/
Best case:
n pushes → Time complexity: O(n)
...
Advice
1
vote
1
replies
42
views
Why do B-tree disk optimizations work when the OS controls physical disk layout?
I understand the standard explanation for why B-trees are used in databases: they minimize disk seeks by packing many keys into each node, keeping the tree shallow (3-4 levels), and enabling efficient ...
1
vote
1
answer
109
views
Sometimes wrong output in Java queue simulation
Problem summary:
Each person in line has tickets[i] tickets to buy. Every second
The person at the front buys one ticket.
If they still need more tickets, they move to the end of the queue.
We need ...
-2
votes
1
answer
38
views
Maximize Score based Prime Score Algorithm
Consider the following LeetCode problem:
You are given an array nums of n positive integers and an integer k. Initially, you start with a score of 1. You have to maximize your score by applying the ...
1
vote
1
answer
168
views
How can I update values in a nested JSON file using Python? [closed]
I have a JSON file with nested objects, and I want to update specific values inside it using Python.
The structure can vary, but it usually looks something like this:
{
"user": {
"...
-4
votes
1
answer
64
views
How to precompute nested date ranges efficiently to optimize range filtering and pagination? [closed]
📝 Body
I have a Mongo collection CollectionA where each top-level object contains a nested array of meetings now each meetings have start and end times, for example:
CollectionA = [
{
&...
-1
votes
1
answer
135
views
How can I merge two lists in Python while removing duplicates but preserving the original order? [duplicate]
I have two lists in Python:
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
I want to merge them into a single list such that:
No duplicates remain
The original order of elements is preserved
For the ...
1
vote
1
answer
58
views
Is circular linked list needed for "Circular" queue?/
I am learning about queue data structure in python. I learnt the implementation of a queue using list in python and the issue of memory wastage when we dequeue a few elements from the front. We use a ...
2
votes
2
answers
103
views
Mutual Friends using Graph Data Structure
I’m trying to implement a mutual friends feature using a graph data structure represented as an adjacency list in Python.
Each node represents a person, and each edge represents a friendship (an ...
1
vote
2
answers
102
views
Is there a bug in the method below (Data Structures & Algorithms by Robert Lafore)
The method below has been taken from the book Data Structures & Algorithms by Robert Lafore. This method is used to bubble sort a given array. nElems is the number of elements in the array. In my ...
1
vote
1
answer
83
views
Looking for a generic data structure being able to describe complex domain specific filters ( or standardized query language )
I'm searching for a very flexible data structure being able to describe what data to fetch from an API (assuming I would own that API by creating a custom wrapper plugin). The data itself is quite ...
3
votes
2
answers
558
views
Find employees with overlapping wortktime hours
Given StartTime and endTime for N employees, an employee can form a team if his working hour overlaps with other employees' (both startTime and endTime inclusive).
Find the maximum team size.
Example:
...
4
votes
2
answers
401
views
Number of lexicographical swaps to make the array non decreasing
A lexicographical swap is defined as:
At each step, pick the earliest possible inversion (i, j) (smallest i, then smallest j > i with a[i] > a[j]) and swap them.
Repeat until the array is sorted....
1
vote
1
answer
167
views
LeetCode 199. Binary Tree Right Side View constant memory complexity
I'm trying to solve LeetCode 199.
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
I've ...
2
votes
1
answer
201
views
Subset data structure with O(1) insert, find, and delete_greater_equal
An abstract data type maintains a subset of numbers {0,1,2,...,n-1}. The operations allowed on this are:
insert(i) - Insert element i in the subset if not already present.
find(i) - Return True iff ...
10
votes
3
answers
621
views
Better way to search for a node in binary tree
This is the code I have written for searching a node in a tree as a part of my assignment:
typedef struct NODE
{
int key;
struct NODE *left, *right;
} Node;
Node *search(Node *head, int key)...
4
votes
2
answers
277
views
Answering Subarray H-index Queries Efficiently
I'm working on an interesting question about answering range h-index queries.
Fyi h-index is the max integer h that an author has published at least h papers that each are cited at least h times.
You ...
2
votes
1
answer
130
views
Saving embeddings from encoder efficiently with fast random access
I have embeddings (about 160 Million) that I created with a BERT-based encoder model.
Right now they are in a .pt format and takes about 500GB in the disk.
I want 2 things:
To save them in an ...
5
votes
1
answer
269
views
TypeCasting Math.pow(2,31) to int is not wrapping around as expected? [duplicate]
I was trying to check for wrap around property when the value is greater than the container that can hold it.
public class Main {
public static void main(String[] args) {
double value = ...
3
votes
2
answers
163
views
Mixed-type hash table design
I think this is a design problem. I’m trying to implement a hash table library in C89 in which the user will be able to insert mixed-type literal keys and values, e.g., HT_SET_LITERAL(&ht, "...
0
votes
0
answers
66
views
Database or data structure for fast exact counting
In databases such as PostgreSQL, doing exact counts like select count (*) from table where condition perform a full table scan.
PostgreSQL will need to scan either the entire table or the entirety
of ...
1
vote
1
answer
125
views
dynamically resizing array in queue implementation [closed]
I'm trying to write a queue that will store strings. Using GDB I can tell that I make a memory allocation error in the function resizeQueue.
Here's the exact message:
Program received signal SIGTRAP, ...
0
votes
0
answers
33
views
How to implement FIFO queue operations in DolphinDB similar to Python’s deque?
In Python, we can use collections.deque to implement FIFO queue operations. For example:
from collections import deque
queue = deque([1, 2, 3])
print("Initial queue:", queue) # ...
0
votes
1
answer
135
views
How to properly pass a data structure pointer from C++ to C#
I have a C++ dll that collects the data from our hardware. I am trying to get a pointer to that data from the C++ dll into C#. Here is the C++ data structure
#pragma pack (push, 1)
typedef struct ...
0
votes
0
answers
74
views
Non-bitwise move or `Move` trait? [duplicate]
In Rust, how can I define&manipulate objects that cannot be just copied bit-by-bit when they're moved?
For example, an object that contains a relative pointer (i.e. a pointer whose target is ...
4
votes
1
answer
84
views
A wait-free consensus algorithm for three processes, with a swap object and the fetch-and-increment object together in one atomic step
We know that a swap object consists of a shared register and supports a swap operation between the shared register and any local register, which atomically exchanges the values of the two registers.
A ...
0
votes
2
answers
322
views
RocksDB for efficient storage and retrieval of keys only (no values)
I have a use case requiring the reliable, durable storage, and efficient retrieval, of a large number of index entries (for which I've an application-specific serialization to bytes that preserves ...
3
votes
1
answer
105
views
Generating Recursive Data Structure From Table
I'm trying to convert this Wikipedia table into a nested JSON object.
I want to group each entry into its parent category. e.g.,
{
"name": "Mining, quarrying, and oil and gas ...
0
votes
0
answers
97
views
How to construct an AVL tree from a set of keys: 10, 20, 5, 4, 3, 2, 30, 40?
I'm trying to understand how an AVL tree is built from a sequence of integer keys. The keys I have are:
10, 20, 5, 4, 3, 2, 30, 40
I want to construct the AVL tree step by step and see how rotations ...
0
votes
1
answer
272
views
Contiguous type-agnostic pointer-data-structure implementation in C?
I am implementing search-trees and hash-tables in pure C (C89). Although the time+space asymptotic complexity is good, the constants and cache-efficiency… not so much.
The keys and values are void* ...
1
vote
2
answers
183
views
Does the type information of an object also take up space in memory?
I'm trying to understand how type information is stored in memory.
We often say that an 8-bit value can store 2⁸ = 256 different values. But I'm wondering — when we create an object of that type, ...
0
votes
1
answer
107
views
Is there a way to store Trie in chrome extension with a deeply nested root?
I'm trying to create a chrome extension where the user adds text snippets to the extension, then when they type in textarea/input/contenteditable/rich-text editors a selection dropdown appears with ...
0
votes
0
answers
121
views
Skip List with each column having an integer instead of copying objects
I tried my best implementing a Skip List in C++ in which I had a particular idea that I wanted to try out. Instead having every element in the base level be a column vector, I only only have a singly-...
2
votes
1
answer
79
views
Rotation of square Matrix not working correctly after first rotation
Please check the below code, the mat array after the second rotation should be identical to the target array tar. However, it is not happening. Could you please review to check where the logic is ...
2
votes
4
answers
190
views
How can I efficiently maintain median in a dynamic data stream with support for deletions?
I'm working on a problem where I need to maintain the median of a data stream, but unlike typical implementations that only support insertions, I also need to support deletions of arbitrary values.
...
0
votes
1
answer
63
views
Why Is My Leaderboard Showing Incorrect Ranks for Tied Scores(Python)?
I’m implementing a game leaderboard in Python where players can have the same score (ties), and I need to support three operations efficiently:
add_score player_id score — update a player’s score (...
-1
votes
1
answer
135
views
Kruskal's Algorithm Minimum Spanning Tree (Disjoint Set data structure)
I would like to generate a minimum spanning tree for the following graph:
I am using the disjoint set data structure and I am having trouble with the union (Union Rank) operation.
The edges get ...
1
vote
0
answers
56
views
plibsys data structures give incorrect results
I am trying to put together some code to find duplicated files between two or more directories, for this
I am using C libs like tinydir, and plibsys, the idea is traverse dirs get the hash of every ...
1
vote
3
answers
114
views
Is this doubly linked list function implemented correctly?
I was watching a youtube video on doubly linked lists in C. One of the functions made for the doubly linked list was an "insert_after_node" function, which as the name suggests inserts a new ...