• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 08:36
CEST 14:36
KST 21:36
  • 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
[ASL21] Ro16 Preview Pt2: All Star10Team Liquid Map Contest #22 - The Finalists16[ASL21] Ro16 Preview Pt1: Fresh Flow9[ASL21] Ro24 Preview Pt2: News Flash10[ASL21] Ro24 Preview Pt1: New Chaos0
Community News
2026 GSL Season 1 Qualifiers19Maestros of the Game 2 announced92026 GSL Tour plans announced15Weekly Cups (April 6-12): herO doubles, "Villains" prevail1MaNa leaves Team Liquid25
StarCraft 2
General
MaNa leaves Team Liquid Maestros of the Game 2 announced 2026 GSL Tour plans announced Team Liquid Map Contest #22 - The Finalists Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool
Tourneys
2026 GSL Season 1 Qualifiers INu's Battles#14 <BO.9 2Matches> Sparkling Tuna Cup - Weekly Open Tournament GSL CK: More events planned pending crowdfunding RSL Revival: Season 5 - Qualifiers and Main Event
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
Mutation # 522 Flip My Base The PondCast: SC2 News & Results Mutation # 521 Memorable Boss Mutation # 520 Moving Fees
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ FlaSh: This Will Be My Final ASL【ASL S21 Ro.16】 BW General Discussion ASL21 General Discussion Data needed
Tourneys
Escore Tournament StarCraft Season 2 [Megathread] Daily Proleagues [ASL21] Ro16 Group C [ASL21] Ro16 Group D
Strategy
Simple Questions, Simple Answers What's the deal with APM & what's its true value Any training maps people recommend? Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread Dawn of War IV Diablo IV Total Annihilation Server - TAForever Starcraft Tabletop Miniature Game
Dota 2
The Story of Wings Gaming
League of Legends
G2 just beat GenG in First stand
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 Five o'clock TL Mafia
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread YouTube Thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece Anime Discussion Thread [Req][Books] Good Fantasy/SciFi books Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion McBoner: A hockey love story Cricket [SPORT]
World Cup 2022
Tech Support
Strange computer issues (software) [G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Sexual Health Of Gamers
TrAiDoS
lurker extra damage testi…
StaticNine
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Iranian anarchists: organize…
XenOsky
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1524 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 Map Contest Tou…
11:00
Playoffs Day 3
Classic vs SHIN
MaxPax vs Percival
herO vs Clem
ByuN vs Rogue
WardiTV1019
Ryung 773
IntoTheiNu 416
Rex123
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Ryung 773
SortOf 188
Rex 123
Railgan 84
StarCraft: Brood War
Horang2 20705
Sea 16276
Hyuk 1129
EffOrt 528
Stork 404
actioN 298
Hyun 262
Last 186
Pusan 159
ggaemo 126
[ Show more ]
ToSsGirL 122
Backho 84
[sc1f]eonzerg 55
Sharp 50
Barracks 44
Free 35
HiyA 26
soO 25
Sacsri 24
yabsab 21
JulyZerg 19
GoRush 16
Noble 9
Icarus 4
Dota 2
Gorgc4351
Counter-Strike
allub451
Heroes of the Storm
Khaldor265
Other Games
singsing1903
B2W.Neo873
Lowko293
DeMusliM281
XBOCT227
XcaliburYe154
RotterdaM35
MindelVK21
Organizations
Dota 2
PGL Dota 2 - Main Stream15859
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
[ Show 13 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• FirePhoenix3
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos1953
• TFBlade1415
Upcoming Events
Ladder Legends
2h 24m
Bunny vs GgMaChine
ByuN vs Percival
MaxPax vs Krystianer
Solar vs Cham
uThermal 2v2 Circuit
2h 24m
BSL
6h 24m
Sparkling Tuna Cup
21h 24m
WardiTV Map Contest Tou…
22h 24m
Ladder Legends
1d 2h
BSL
1d 6h
CranKy Ducklings
1d 11h
Replay Cast
1d 20h
Wardi Open
1d 21h
[ Show More ]
Afreeca Starleague
1d 21h
Soma vs hero
Monday Night Weeklies
2 days
Replay Cast
2 days
Replay Cast
2 days
Afreeca Starleague
2 days
Leta vs YSC
Replay Cast
4 days
The PondCast
4 days
KCM Race Survival
4 days
Replay Cast
5 days
Replay Cast
5 days
Escore
5 days
Replay Cast
6 days
Replay Cast
6 days
Liquipedia Results

Completed

Escore Tournament S2: W4
RSL Revival: Season 4
NationLESS Cup

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
KCM Race Survival 2026 Season 2
StarCraft2 Community Team League 2026 Spring
WardiTV TLMC #16
Nations Cup 2026
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
PGL Cluj-Napoca 2026

Upcoming

Escore Tournament S2: W5
Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
Maestros of the Game 2
2026 GSL S2
RSL Revival: Season 5
2026 GSL S1
XSE Pro League 2026
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
IEM Atlanta 2026
Asian Champions League 2026
PGL Astana 2026
BLAST Rivals Spring 2026
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.