• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 09:40
CET 15:40
KST 23:40
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
ByuL: The Forgotten Master of ZvT26Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13Rongyi Cup S3 - Preview & Info8
Community News
Weekly Cups (Feb 16-22): MaxPax doubles0Weekly Cups (Feb 9-15): herO doubles up2ACS replaced by "ASL Season Open" - Starts 21/0242LiuLi Cup: 2025 Grand Finals (Feb 10-16)46Weekly Cups (Feb 2-8): Classic, Solar, MaxPax win2
StarCraft 2
General
Oliveira Would Have Returned If EWC Continued ByuL: The Forgotten Master of ZvT Behind the Blue - Team Liquid History Book Weekly Cups (Feb 16-22): MaxPax doubles How do you think the 5.0.15 balance patch (Oct 2025) for StarCraft II has affected the game?
Tourneys
StarCraft Evolution League (SC Evo Biweekly) Sea Duckling Open (Global, Bronze-Diamond) PIG STY FESTIVAL 7.0! (19 Feb - 1 Mar) Sparkling Tuna Cup - Weekly Open Tournament How do the "codes" work in GSL?
Strategy
Custom Maps
Map Editor closed ? [A] Starcraft Sound Mod
External Content
Mutation # 514 Ulnar New Year The PondCast: SC2 News & Results Mutation # 513 Attrition Warfare Mutation # 512 Overclocked
Brood War
General
ACS replaced by "ASL Season Open" - Starts 21/02 TvZ is the most complete match up CasterMuse Youtube A cwal.gg Extension - Easily keep track of anyone A new season just kicks off
Tourneys
Escore Tournament StarCraft Season 1 [Megathread] Daily Proleagues [LIVE] [S:21] ASL Season Open Day 1 Small VOD Thread 2.0
Strategy
Fighting Spirit mining rates Simple Questions, Simple Answers Zealot bombing is no longer popular? Current Meta
Other Games
General Games
Nintendo Switch Thread Beyond All Reason Battle Aces/David Kim RTS Megathread New broswer game : STG-World Diablo 2 thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
US Politics Mega-thread Mexico's Drug War Russo-Ukrainian War Thread Canadian Politics Mega-thread Ask and answer stupid questions here!
Fan Clubs
The IdrA Fan Club The herO Fan Club!
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Anime Discussion Thread
Sports
Formula 1 Discussion 2024 - 2026 Football Thread TL MMA Pick'em Pool 2013
World Cup 2022
Tech Support
TL Community
The Automated Ban List
Blogs
ASL S21 English Commentary…
namkraft
Inside the Communication of …
TrAiDoS
My 2025 Magic: The Gathering…
DARKING
Life Update and thoughts.
FuDDx
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2155 users

Logic/Programming Interview Questions - Page 3

Blogs > Oracle
Post a Reply
Prev 1 2 3 All
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
June 27 2011 14:05 GMT
#41
Frigo, the O(1) space requirement implies that the additional space we need isn't dependent on how large the input is. If you are using 1 extra bit for every node, that could theoretically be 2^100 more bits. That's why there is a significant difference between in-place and non in-place sorting algorithms: If you have a petabyte of data and a petabyte of storage space, you're not going anywhere without an in-place algorithm.

But given the way the question is worded, we have no guarantee about the current state of the pointers, as an adversary could defeat you every time on the first node.
Compilers are like boyfriends, you miss a period and they go crazy on you.
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 20:44:45
June 27 2011 20:42 GMT
#42
Sorry I havent checked this in a while, here's some clarifications:

I will post a diagram for Q2 soon

BottleAbuser, your proof of the O(n) running time of k-select is fine with me now

I have a different solution:
You turn the array into a heap and then find the k-th largest. I'll post a detailed solution soon.

For Q6:
You can assume that node->next modulo 4 == 0 for every node in the list. You guys are actually really close to the answer.

Frigo, your answer to Q7 doesn't seem right with me, you can refer to the post I made before regarding a similar method.
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:42:40
June 27 2011 21:26 GMT
#43
heapify(A)
A: an array

1. n ← size(A) − 1
2. for i ← floor(n/2) to 0 do
3. bubble−up(A, i)

Builds a max-heap in linear-time, given the assumption that the 2i'th index is the left child, and 2i+1'th index is the right child of an element.

Then its a simple o(k) time to find the kth largest
Hamster1800
Profile Blog Joined August 2008
United States175 Posts
June 27 2011 22:02 GMT
#44
Could you elaborate more on how to select once you've heapified the array? I'm not familiar with a linear time method to do k-select in a heap. BottleAbuser's method (recurse on median-of-medians) I believe is the standard method for k-select.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:37:46
June 27 2011 22:09 GMT
#45
You do sort of a breadth first search on the heap, giving priority to the greatest element and traverse that node. It comes out to O(k)
Hamster1800
Profile Blog Joined August 2008
United States175 Posts
June 27 2011 22:19 GMT
#46
That shouldn't work. The relative size of two nodes says nothing about the relative sizes of their children. For example, just because the top of the (min-)heap is 1 with children 10 and 100, the 20th smallest element might be the 100. It also could be some deep descendent of the 10 or a deep descendent of the 100. I don't understand how you're going to determine which is the case.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-27 22:44:38
June 27 2011 22:22 GMT
#47
A max heap is a left-justified binary tree. The invariant on the tree is that a node's key is strictly greater than or equal to each of its children nodes' key.

We traverse only k-1 edges, and every time we traverse an edge we will effectively be eliminating a greater element than k. But we only traverse the edge with the child with the greatest key.

Hence we place each key in a priority queue, and traverse the greatest key out of all the "active" nodes.

But since we only traverse k-1 edges, we are done at the kth iteration.


In Pseudocode,

activenode <- root
initialize Maxheap Q

for 0 to k-1
put children of activenode into Q
activenode <- get max of Q

return activenode

Actually i guess this is O(klogk) but I believe theres an analysis which shows its within n so i'll think about it for a bit. (Something to do with Q's size being at most n/2 iirc, guess I shouldve done the problems before posting them )

Hamster1800
Profile Blog Joined August 2008
United States175 Posts
Last Edited: 2011-06-27 22:27:47
June 27 2011 22:27 GMT
#48
Let me be more clear: I'm not worried about the runtime of your algorithm. I'm worried about what it is and that it gets the correct answer. If you could explain your k-select method in full and explain how it gets the kth largest element, that would answer my question.
D is for Diamond, E is for Everything Else
Oracle
Profile Blog Joined May 2007
Canada411 Posts
June 27 2011 22:38 GMT
#49
Ive edited my post above to be more clear
Frigo
Profile Joined August 2009
Hungary1023 Posts
Last Edited: 2011-06-28 03:02:28
June 28 2011 02:59 GMT
#50
On June 28 2011 05:42 Oracle wrote:
For Q6:
You can assume that node->next modulo 4 == 0 for every node in the list. You guys are actually really close to the answer.

Well it's settled then. We just need to set a bit in the "next" pointer to mark a node visited. And technically, the algorithm uses only an O(1) space overhead, not O(n), made possible by the special structure of the list.

Frigo, your answer to Q7 doesn't seem right with me, you can refer to the post I made before regarding a similar method.

Well, I have only shown that we convey enough information to tell the 5th card, I left the ordering and indexing of the cards, and the method to calculate the index of the 5th from the possible arrangements, implicit.

Other than that, I forgot the case when all cards are upside down:
0 upside down, 4 visible: 4 choose 0 * 4! = 1 * 24 = 24
1 upside down, 3 visible: 4 choose 1 * 3! = 4 * 6 = 24
2 upside down, 2 visible: 4 choose 2 * 2! = 6 * 2 = 12
3 upside down, 1 visible: 4 choose 3 * 1! = 4 * 1 = 4
4 upside down, 0 visible: 4 choose 4 * 0! = 1 * 1 = 1
65 possible arrangements
http://www.fimfiction.net/user/Treasure_Chest
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
Last Edited: 2011-06-28 11:17:25
June 28 2011 11:15 GMT
#51
Well, we can simply increment each traversed node's next pointer to mark it as visited then. If the next node'spointer mod 4 is 1 then that's the start of the loop.

Should read whole thread heh. I meant "what frigo said"
Compilers are like boyfriends, you miss a period and they go crazy on you.
iaretehnoob
Profile Joined June 2004
Sweden741 Posts
June 28 2011 12:15 GMT
#52
On June 25 2011 09:32 Oracle wrote:
Here's mine:

We think of a card as a {0,1}-bit. A card represents 0 if it is face up and 1 if it is face-down.

So then we get a binary string of length four. So we can represent 2^4 - 1 = 15 possibly values with a string of 4 bits. But there are really only 13 distinct values. Hence we will never get the string 1111. So we will always have a face-up card.

So we assign 'a' = Hearts, 'b' = Diamonds, 'c' = Spades, 'd' = Clubs. The card which is face-up and leftmost denotes the suit.


So for example A F F F can be read as 0111 = 7. The A says its the 7 of Hearts.
FBDF can be read as 1001 = 9. The B says its the 9 of Diamonds.

Maybe I'm just misunderstanding your method, but doesn't this assume you actually get at least one other card of the same suit as the fifth card? For example how would you represent the 7 of Hearts if the other 4 cards are black?
Oracle
Profile Blog Joined May 2007
Canada411 Posts
Last Edited: 2011-06-28 15:02:31
June 28 2011 15:01 GMT
#53
On June 28 2011 21:15 iaretehnoob wrote:
Show nested quote +
On June 25 2011 09:32 Oracle wrote:
Here's mine:

We think of a card as a {0,1}-bit. A card represents 0 if it is face up and 1 if it is face-down.

So then we get a binary string of length four. So we can represent 2^4 - 1 = 15 possibly values with a string of 4 bits. But there are really only 13 distinct values. Hence we will never get the string 1111. So we will always have a face-up card.

So we assign 'a' = Hearts, 'b' = Diamonds, 'c' = Spades, 'd' = Clubs. The card which is face-up and leftmost denotes the suit.


So for example A F F F can be read as 0111 = 7. The A says its the 7 of Hearts.
FBDF can be read as 1001 = 9. The B says its the 9 of Diamonds.

Maybe I'm just misunderstanding your method, but doesn't this assume you actually get at least one other card of the same suit as the fifth card? For example how would you represent the 7 of Hearts if the other 4 cards are black?

The 1st card I pulled out is mapped to hearts, regardless if it is a black-suited card. So we map the ordering not the suit.


On June 28 2011 20:15 BottleAbuser wrote:
Well, we can simply increment each traversed node's next pointer to mark it as visited then. If the next node'spointer mod 4 is 1 then that's the start of the loop.

Should read whole thread heh. I meant "what frigo said"



Right, I love this one
Prev 1 2 3 All
Please log in or register to reply.
Live Events Refresh
WardiTV Winter Champion…
12:00
Group C
WardiTV1031
IndyStarCraft 195
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Lowko446
IndyStarCraft 195
ProTech120
Vindicta 43
StarCraft: Brood War
Britney 42660
firebathero 4399
Horang2 4283
Hyuk 1561
Jaedong 928
Light 458
Rush 165
hero 110
Sea.KH 89
ToSsGirL 77
[ Show more ]
[sc1f]eonzerg 61
sorry 45
Hm[arnc] 35
scan(afreeca) 14
Terrorterran 13
Noble 12
Dota 2
Gorgc4470
qojqva1927
XcaliburYe157
febbydoto15
Counter-Strike
olofmeister2235
allub367
byalli161
markeloff129
oskar75
kRYSTAL_52
Super Smash Bros
Mew2King116
Heroes of the Storm
crisheroes172
Other Games
B2W.Neo916
hiko499
QueenE158
XaKoH 106
DeMusliM104
Liquid`VortiX85
Sick59
Organizations
Counter-Strike
PGL749
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 15 non-featured ]
StarCraft 2
• StrangeGG 29
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• HerbMon 2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV484
League of Legends
• Nemesis5168
• TFBlade901
Upcoming Events
Replay Cast
18h 20m
CasterMuse Showmatch
18h 20m
Light vs Queen
WardiTV Winter Champion…
21h 20m
The PondCast
1d 19h
Replay Cast
2 days
Korean StarCraft League
3 days
CranKy Ducklings
3 days
SC Evo Complete
3 days
Replay Cast
4 days
Sparkling Tuna Cup
4 days
[ Show More ]
uThermal 2v2 Circuit
5 days
Replay Cast
5 days
Wardi Open
5 days
Replay Cast
6 days
Liquipedia Results

Completed

Proleague 2026-02-22
LiuLi Cup: 2025 Grand Finals
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Acropolis #4 - TS5
Jeongseon Sooper Cup
Spring Cup 2026
WardiTV Winter 2026
PiG Sty Festival 7.0
Nations Cup 2026
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025

Upcoming

[S:21] ASL SEASON OPEN 2nd Round
[S:21] ASL SEASON OPEN 2nd Round Qualifier
Acropolis #4 - TS6
Acropolis #4
IPSL Spring 2026
HSC XXIX
uThermal 2v2 2026 Main Event
Bellum Gens Elite Stara Zagora 2026
RSL Revival: Season 4
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
FISSURE Playground #3
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.